GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cache/invalidation_test.go at main · ubgo/cache Expand file tree/ Copy pathinvalidation_test.goMore file actions 75 lines (68 loc) · 1.7 KB / Copy pathinvalidation_test.goFile metadata and controls75 lines (68 loc) · 1.7 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 // invalidation_test.go — tests for the in-process invalidation bus (invalidation.go). package cache_test import ( "context" "sync" "testing" "time" "github.com/ubgo/cache" ) func TestInProcInvalidationFanout(t *testing.T) { bus := cache.NewInProcInvalidation() ctx, cancel := context.WithCancel(context.Background()) defer cancel() var mu sync.Mutex got := map[int][]string{} var ready sync.WaitGroup for i := 0; i < 3; i++ { ready.Add(1) go func(id int) { started := false _ = bus.Subscribe(ctx, func(k string) { if !started { started = true } mu.Lock() got[id] = append(got[id], k) mu.Unlock() }) }(i) } // Give subscribers a moment to register. time.Sleep(50 * time.Millisecond) if err := bus.Publish(context.Background(), "a", "b"); err != nil { t.Fatal(err) } deadline := time.Now().Add(2 * time.Second) for { mu.Lock() done := len(got) == 3 && len(got[0]) == 2 && len(got[1]) == 2 && len(got[2]) == 2 mu.Unlock() if done || time.Now().After(deadline) { break } time.Sleep(10 * time.Millisecond) } mu.Lock() defer mu.Unlock() for id := 0; id < 3; id++ { if len(got[id]) != 2 || got[id][0] != "a" || got[id][1] != "b" { t.Fatalf("subscriber %d got %v, want [a b]", id, got[id]) } } } func TestInProcSubscribeStopsOnContext(t *testing.T) { bus := cache.NewInProcInvalidation() ctx, cancel := context.WithCancel(context.Background()) done := make(chan error, 1) go func() { done <- bus.Subscribe(ctx, func(string) {}) }() cancel() select { case err := <-done: if err == nil { t.Fatal("Subscribe should return ctx error on cancel") } case <-time.After(time.Second): t.Fatal("Subscribe did not return after context cancel") } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |