File: chans_test.go

package info (click to toggle)
golang-github-bradenaw-juniper 0.15.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 872 kB
  • sloc: sh: 27; makefile: 2
file content (143 lines) | stat: -rw-r--r-- 2,455 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package chans

import (
	"math/rand"
	"runtime"
	"sync/atomic"
	"testing"
	"time"

	"github.com/bradenaw/juniper/internal/require2"
	"github.com/bradenaw/juniper/xslices"
)

func FuzzMerge(f *testing.F) {
	f.Fuzz(func(t *testing.T, n int, b []byte) {
		if n > 5 || n <= 0 {
			return
		}

		t.Logf("n = %d", n)

		out := make(chan byte)
		ins := make([]chan byte, n)
		for i := range ins {
			ins[i] = make(chan byte)
		}
		ins2 := xslices.Map(ins, func(c chan byte) <-chan byte { return c })

		go func() {
			Merge(out, ins2...)
			close(out)
		}()

		var inSlice []byte
		var outSlice []byte
		done := make(chan struct{})
		go func() {
			for item := range out {
				outSlice = append(outSlice, item)
			}
			close(done)
		}()

	Loop:
		for {
			if len(b) < 3 {
				break
			}
			idx := int(b[0])
			if idx >= len(ins) {
				break
			}
			switch b[1] {
			case 0:
				inSlice = append(inSlice, b[2])
				ins[idx] <- b[2]
			case 1:
				close(ins[idx])
				ins = xslices.RemoveUnordered(ins, idx, 1)
			default:
				break Loop
			}
			b = b[3:]
		}
		for _, in := range ins {
			close(in)
		}
		<-done

		require2.SlicesEqual(t, inSlice, outSlice)
	})
}

func TestStressMerge(t *testing.T) {
	t.Skip()
	count := uint64(0)
	start := time.Now()

	go func() {
		for {
			t.Logf("%s %d", time.Since(start).Round(time.Second), count)
			time.Sleep(3 * time.Second)
		}
	}()

	for i := 0; i < runtime.GOMAXPROCS(-1); i++ {
		go func() {
			r := rand.New(rand.NewSource(time.Now().Unix()))

			for {
				n := r.Intn(4) + 1

				atomic.AddUint64(&count, 1)

				out := make(chan byte)
				ins := make([]chan byte, n)
				for i := range ins {
					ins[i] = make(chan byte)
				}

				ins2 := xslices.Map(ins, func(c chan byte) <-chan byte { return c })
				go func() {
					Merge(out, ins2...)
					close(out)
				}()

				var inS []byte
				var outS []byte
				done := make(chan struct{})
				go func() {
					for item := range out {
						outS = append(outS, item)
					}
					close(done)
				}()

				for {
					if len(ins) == 0 {
						break
					}
					idx := r.Intn(len(ins))
					switch r.Intn(2) {
					case 0:
						v := byte(r.Intn(256))
						inS = append(inS, v)
						ins[idx] <- v
					case 1:
						close(ins[idx])
						nBefore := len(ins)
						ins = xslices.RemoveUnordered(ins, idx, 1)
						require2.Equal(t, len(ins), nBefore-1)
					}
				}
				<-done

				require2.SlicesEqual(t, inS, outS)
			}
		}()
	}

	c := make(chan struct{})
	<-c
}