-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpipe_test.go
114 lines (102 loc) · 1.87 KB
/
pipe_test.go
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
//
// pipe_test.go
//
// Copyright (c) 2023-2024 Markku Rossi
//
// All rights reserved.
//
package ot
import (
"bytes"
"fmt"
"io"
"testing"
)
func TestPipe(t *testing.T) {
var tests = []interface{}{
byte('@'),
42,
[]byte("Hello, world!"),
}
pipe, rPipe := NewPipe()
done := make(chan error)
go func(pipe *Pipe) {
for _, test := range tests {
switch v := test.(type) {
case byte:
val, err := pipe.ReceiveByte()
if err != nil {
done <- err
pipe.Close()
return
}
if val != v {
done <- fmt.Errorf("ReceiveByte: mismatch: %v != %v",
val, v)
pipe.Close()
return
}
case int:
val, err := pipe.ReceiveUint32()
if err != nil {
done <- err
pipe.Close()
return
}
if val != v {
done <- fmt.Errorf("ReceiveUint32: mismatch: %v != %v",
val, v)
pipe.Close()
return
}
case []byte:
data, err := pipe.ReceiveData()
if err != nil {
done <- err
pipe.Close()
return
}
if bytes.Compare(data, v) != 0 {
done <- fmt.Errorf("ReceiveData: mismatch: %x != %x",
data, v)
pipe.Close()
return
}
default:
panic(fmt.Sprintf("receive %v(%T) not supported", v, v))
}
}
_, err := pipe.ReceiveUint32()
if err != io.EOF {
done <- fmt.Errorf("expected EOF")
}
done <- nil
}(rPipe)
for _, test := range tests {
switch v := test.(type) {
case byte:
err := pipe.SendByte(v)
if err != nil {
t.Errorf("SendByte failed: %v", err)
}
case int:
err := pipe.SendUint32(v)
if err != nil {
t.Errorf("SendUint32 failed: %v", err)
}
case []byte:
err := pipe.SendData(v)
if err != nil {
t.Errorf("SendData failed: %v", err)
}
}
}
err := pipe.Close()
if err != nil {
t.Errorf("Close failed: %v", err)
}
err = <-done
if err != nil {
t.Errorf("consumer failed: %v", err)
}
}