This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclient_test.go
78 lines (69 loc) · 1.6 KB
/
client_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
package main
import (
"bytes"
)
type testReadWriter struct {
readChannel chan []byte
writeChannel chan []byte
}
type testError struct {
message string
}
func (e *testError) Error() string {
return e.message
}
func (rw *testReadWriter) Read(p []byte) (n int, err error) {
buffer, ok := <-rw.readChannel
if !ok {
return 0, &testError{"Closed"}
}
for i := range(buffer) {
p[i] = buffer[i]
}
return len(buffer), nil
}
func (rw *testReadWriter) Write(p []byte) (n int, err error) {
rw.writeChannel <- p
return len(p), nil
}
func (rw *testReadWriter) Close() {
close(rw.readChannel)
}
func echoHandler(requestChannel chan []byte, responseChannel chan []byte, closeChannel chan bool) {
buffer := <-requestChannel
buffer2 := <-requestChannel
responseChannel <- bytes.Join([][]byte{buffer, buffer2}, []byte{})
closeChannel <- true
}
/*
func TestRPC(t *testing.T) {
readWriter := &testReadWriter {
readChannel: make(chan []byte, 2),
writeChannel: make(chan []byte),
}
m := NewRPCMultiplexer(readWriter, echoHandler)
go m.Multiplex()
fmt.Println("Go routines", runtime.NumGoroutine())
var i byte
for i = 0; i < 5; i++ {
testBuffer := []byte{i, i+1, i+2, i+3}
readWriter.readChannel <- testBuffer
}
for i = 0; i < 5; i++ {
testBuffer := []byte{i, i+4, i+5, i+6}
readWriter.readChannel <- testBuffer
}
for i = 0; i < 5; i++ {
response := <-readWriter.writeChannel
base := response[0]
var j byte
for j = 1; j < byte(len(response)); j++ {
if response[j] != base + j {
t.Fail()
}
}
fmt.Println("Got back", response)
}
fmt.Println("Go routines", runtime.NumGoroutine())
}
*/