(t *testing.T)
| 17 | } |
| 18 | |
| 19 | func TestChangeCacheConcurrent(t *testing.T) { |
| 20 | t.Parallel() |
| 21 | |
| 22 | ctx := t.Context() |
| 23 | c := newChangeCache() |
| 24 | |
| 25 | var initCount atomic.Int32 |
| 26 | initStarted := make(chan struct{}) |
| 27 | initDone := make(chan struct{}) |
| 28 | |
| 29 | results := make([]*cachedChange, 0, 100) |
| 30 | var resultsMu sync.Mutex |
| 31 | |
| 32 | var wg sync.WaitGroup |
| 33 | for range 100 { |
| 34 | wg.Add(1) |
| 35 | go func() { |
| 36 | defer wg.Done() |
| 37 | res, err := c.getOrInit(ctx, "42", func(_ context.Context) (*ChangeWithStat, error) { |
| 38 | if initCount.Add(1) == 1 { |
| 39 | close(initStarted) |
| 40 | } |
| 41 | <-initDone |
| 42 | return mkChange(ChangeKindModify), nil |
| 43 | }) |
| 44 | assert.NilError(t, err) |
| 45 | assert.Equal(t, ChangeKindModify, res.result().kind) |
| 46 | |
| 47 | resultsMu.Lock() |
| 48 | results = append(results, res) |
| 49 | resultsMu.Unlock() |
| 50 | }() |
| 51 | } |
| 52 | |
| 53 | select { |
| 54 | case <-initStarted: |
| 55 | case <-time.After(5 * time.Second): |
| 56 | t.Fatal("timed out waiting for initializer") |
| 57 | } |
| 58 | close(initDone) |
| 59 | |
| 60 | wg.Wait() |
| 61 | assert.Equal(t, int32(1), initCount.Load()) |
| 62 | |
| 63 | for _, res := range results { |
| 64 | res.release() |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestChangeCacheErrors(t *testing.T) { |
| 69 | t.Parallel() |
nothing calls this directly
no test coverage detected