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
|
func TestSizeBuffer(t *testing.T) { ctx := context.Background()
in := make(chan interface{}) go func() { for i := 0; i < 10; i++ { in <- i } }()
out := BufferChan(ctx, in, BufferConfig{ Size: func() int { return 3 }, Timeout: func() time.Duration { return time.Second }, })
want := []interface{}{ []interface{}{0, 1, 2}, []interface{}{3, 4, 5}, []interface{}{6, 7, 8}, []interface{}{9}} var got []interface{} for i := 0; i < 4; i++ { got = append(got, <-out) }
if !reflect.DeepEqual(got, want) { t.Errorf("%v != %v", got, want) } }
func TestTimeoutBuffer(t *testing.T) { ctx := context.Background()
in := make(chan interface{}) go func() { in <- 0 time.Sleep(time.Millisecond * 500) in <- 1 in <- 2 time.Sleep(time.Millisecond * 500) in <- 3 in <- 4 in <- 5 }()
out := BufferChan(ctx, in, BufferConfig{ Size: func() int { return 100 }, Timeout: func() time.Duration { return time.Millisecond * 300 }, })
want := []interface{}{ []interface{}{0}, []interface{}{1, 2}, []interface{}{3, 4, 5}, } var got []interface{} for i := 0; i < 3; i++ { got = append(got, <-out) }
if !reflect.DeepEqual(got, want) { t.Errorf("%v != %v", got, want) } }
|