(t *testing.T)
| 22 | } |
| 23 | |
| 24 | func TestLoop(t *testing.T) { |
| 25 | th.TestBothOrderings(t, func(t *testing.T, ord bool) { |
| 26 | for _, n := range []int{1, 5} { |
| 27 | t.Run(th.Name("correctness", n), func(t *testing.T) { |
| 28 | in := th.FromRange(0, 20) |
| 29 | done := make(chan struct{}) |
| 30 | |
| 31 | var sum atomic.Int64 |
| 32 | |
| 33 | universalLoop(ord, in, done, n, func(x int, canWrite <-chan struct{}) { |
| 34 | <-canWrite |
| 35 | sum.Add(int64(x)) |
| 36 | }) |
| 37 | |
| 38 | <-done |
| 39 | th.ExpectValue(t, sum.Load(), 19*20/2) |
| 40 | }) |
| 41 | |
| 42 | t.Run(th.Name("concurrency", n), func(t *testing.T) { |
| 43 | in := th.FromRange(0, 100) |
| 44 | out := make(chan int) |
| 45 | |
| 46 | monitor := th.NewConcurrencyMonitor(1 * time.Second) |
| 47 | |
| 48 | universalLoop(ord, in, out, n, func(x int, canWrite <-chan struct{}) { |
| 49 | monitor.Inc() |
| 50 | defer monitor.Dec() |
| 51 | |
| 52 | <-canWrite |
| 53 | |
| 54 | out <- x |
| 55 | }) |
| 56 | |
| 57 | Drain(out) |
| 58 | |
| 59 | th.ExpectValue(t, monitor.Max(), n) |
| 60 | }) |
| 61 | |
| 62 | t.Run(th.Name("ordering", n), func(t *testing.T) { |
| 63 | in := th.FromRange(0, 20000) |
| 64 | out := make(chan int) |
| 65 | |
| 66 | universalLoop(ord, in, out, n, func(x int, canWrite <-chan struct{}) { |
| 67 | |
| 68 | <-canWrite |
| 69 | |
| 70 | out <- x |
| 71 | }) |
| 72 | |
| 73 | outSlice := th.ToSlice(out) |
| 74 | |
| 75 | if ord || n == 1 { |
| 76 | th.ExpectSorted(t, outSlice) |
| 77 | } else { |
| 78 | th.ExpectUnsorted(t, outSlice) |
| 79 | } |
| 80 | |
| 81 | }) |
nothing calls this directly
no test coverage detected