File: resizable_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 (61 lines) | stat: -rw-r--r-- 1,238 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package channels

import (
	"math/rand"
	"testing"
)

func TestResizableChannel(t *testing.T) {
	var ch *ResizableChannel

	ch = NewResizableChannel()
	testChannel(t, "default resizable channel", ch)

	ch = NewResizableChannel()
	testChannelPair(t, "default resizable channel", ch, ch)

	ch = NewResizableChannel()
	ch.Resize(Infinity)
	testChannel(t, "infinite resizable channel", ch)

	ch = NewResizableChannel()
	ch.Resize(Infinity)
	testChannelPair(t, "infinite resizable channel", ch, ch)

	ch = NewResizableChannel()
	ch.Resize(5)
	testChannel(t, "5-buffer resizable channel", ch)

	ch = NewResizableChannel()
	ch.Resize(5)
	testChannelPair(t, "5-buffer resizable channel", ch, ch)

	ch = NewResizableChannel()
	testChannelConcurrentAccessors(t, "resizable channel", ch)
}

func TestResizableChannelOnline(t *testing.T) {
	stopper := make(chan bool)
	ch := NewResizableChannel()
	go func() {
		for i := 0; i < 1000; i++ {
			ch.In() <- i
		}
		<-stopper
		ch.Close()
	}()

	go func() {
		for i := 0; i < 1000; i++ {
			ch.Resize(BufferCap(rand.Intn(50) + 1))
		}
		close(stopper)
	}()

	for i := 0; i < 1000; i++ {
		val := <-ch.Out()
		if i != val.(int) {
			t.Fatal("resizable channel expected", i, "but got", val.(int))
		}
	}
}