|
| 1 | +package testbed |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func Test_perWorkerTickDuration(t *testing.T) { |
| 9 | + for _, test := range []struct { |
| 10 | + name string |
| 11 | + expectedTickDuration time.Duration |
| 12 | + dataItemsPerSecond, itemsPerBatch, numWorkers int |
| 13 | + }{ |
| 14 | + // Because of the way perWorkerTickDuration calculates the tick interval using dataItemsPerSecond, |
| 15 | + // it is important to test its behavior with respect to a one-second Duration in particular. |
| 16 | + { |
| 17 | + name: "less than one second", |
| 18 | + expectedTickDuration: 100 * time.Millisecond, |
| 19 | + dataItemsPerSecond: 100, |
| 20 | + itemsPerBatch: 5, |
| 21 | + numWorkers: 2, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "exactly one second", |
| 25 | + expectedTickDuration: time.Second, |
| 26 | + dataItemsPerSecond: 100, |
| 27 | + itemsPerBatch: 5, |
| 28 | + numWorkers: 20, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "more than one second", |
| 32 | + expectedTickDuration: 5 * time.Second, |
| 33 | + dataItemsPerSecond: 100, |
| 34 | + itemsPerBatch: 5, |
| 35 | + numWorkers: 100, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "default batch size and worker count", |
| 39 | + expectedTickDuration: 8103727, // ~8.1ms |
| 40 | + dataItemsPerSecond: 1234, |
| 41 | + itemsPerBatch: 10, |
| 42 | + numWorkers: 1, |
| 43 | + }, |
| 44 | + } { |
| 45 | + t.Run(test.name, func(t *testing.T) { |
| 46 | + subject := &ProviderSender{ |
| 47 | + options: LoadOptions{ |
| 48 | + DataItemsPerSecond: test.dataItemsPerSecond, |
| 49 | + ItemsPerBatch: test.itemsPerBatch, |
| 50 | + }, |
| 51 | + } |
| 52 | + actual := subject.perWorkerTickDuration(test.numWorkers) |
| 53 | + if actual != test.expectedTickDuration { |
| 54 | + t.Errorf("got %v; want %v", actual, test.expectedTickDuration) |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
0 commit comments