File: batching_channel_test.go

package info (click to toggle)
golang-gopkg-eapache-channels.v1 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, experimental, forky, sid, trixie
  • size: 164 kB
  • sloc: makefile: 2
file content (45 lines) | stat: -rw-r--r-- 863 bytes parent folder | download | duplicates (3)
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
package channels

import "testing"

func testBatches(t *testing.T, ch Channel) {
	go func() {
		for i := 0; i < 1000; i++ {
			ch.In() <- i
		}
		ch.Close()
	}()

	i := 0
	for val := range ch.Out() {
		for _, elem := range val.([]interface{}) {
			if i != elem.(int) {
				t.Fatal("batching channel expected", i, "but got", elem.(int))
			}
			i++
		}
	}
}

func TestBatchingChannel(t *testing.T) {
	ch := NewBatchingChannel(Infinity)
	testBatches(t, ch)

	ch = NewBatchingChannel(2)
	testBatches(t, ch)

	ch = NewBatchingChannel(1)
	testChannelConcurrentAccessors(t, "batching channel", ch)
}

func TestBatchingChannelCap(t *testing.T) {
	ch := NewBatchingChannel(Infinity)
	if ch.Cap() != Infinity {
		t.Error("incorrect capacity on infinite channel")
	}

	ch = NewBatchingChannel(5)
	if ch.Cap() != 5 {
		t.Error("incorrect capacity on infinite channel")
	}
}