z_test.go 897 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package cache
  2. import (
  3. "context"
  4. "log"
  5. "sync"
  6. "testing"
  7. "time"
  8. )
  9. func TestChan(t *testing.T) {
  10. t.Log("!!!")
  11. ch := make(chan int, 1)
  12. for i := 0; i < 5; i++ {
  13. go func(im int) {
  14. for val := range ch {
  15. t.Log(im, val)
  16. }
  17. }(i)
  18. }
  19. for i := 0; i < 10; i++ {
  20. ch <- i
  21. time.Sleep(time.Second * 2)
  22. }
  23. }
  24. func TestContext(t *testing.T) {
  25. ctx, cancel := context.WithCancel(context.Background())
  26. var wg sync.WaitGroup
  27. wg.Add(1)
  28. go func() {
  29. log.Println("OK")
  30. <-ctx.Done()
  31. log.Println("Context done")
  32. //wg.Add(1)
  33. go func() {
  34. log.Println("ST")
  35. <-ctx.Done()
  36. log.Println("Context done 2")
  37. }()
  38. }()
  39. time.Sleep(time.Second * 2)
  40. cancel()
  41. wg.Wait()
  42. }
  43. func TestCacheMap(t *testing.T) {
  44. cm := NewCacheMap(context.Background(), 10, LiveInfinite)
  45. for i := 0; i < 10; i++ {
  46. cm.Set(i, i)
  47. t.Log(cm)
  48. time.Sleep(time.Second)
  49. }
  50. ko := cm.keysOlder(3)
  51. t.Log(ko)
  52. }