This repository has been archived by the owner on Jun 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmultiplexer_test.go
90 lines (83 loc) · 1.98 KB
/
multiplexer_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
package turnc
import (
"io"
"net"
"testing"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
)
type closeFunc func() error
func (f closeFunc) Close() error {
return f()
}
type readFunc func(buf []byte) (int, error)
func (f readFunc) Read(buf []byte) (int, error) {
return f(buf)
}
func TestMultiplexer(t *testing.T) {
t.Run("closeLogged", func(t *testing.T) {
core, logs := observer.New(zap.ErrorLevel)
closeLogged(zap.New(core), "message", closeFunc(func() error {
return io.ErrUnexpectedEOF
}))
if logs.Len() < 1 {
t.Error("no errors logged")
}
})
t.Run("discardLogged", func(t *testing.T) {
core, logs := observer.New(zap.ErrorLevel)
discardLogged(zap.New(core), "message", readFunc(func(buf []byte) (int, error) {
return 0, io.ErrUnexpectedEOF
}))
if logs.Len() < 1 {
t.Error("no errors logged")
}
})
t.Run("AppData", func(t *testing.T) {
core, logs := observer.New(zap.ErrorLevel)
connL, connR := net.Pipe()
m := newMultiplexer(connR, zap.New(core))
go func() {
if err := connL.SetWriteDeadline(time.Now().Add(time.Second)); err != nil {
t.Error(err)
}
if _, err := connL.Write([]byte{1, 2, 3, 4}); err != nil {
t.Error(err)
}
}()
buf := make([]byte, 1024)
if _, err := m.dataL.Read(buf); err != nil {
t.Error(err)
}
if logs.Len() > 0 {
t.Error("no logs expected")
}
})
t.Run("Write error", func(t *testing.T) {
core, logs := observer.New(zap.WarnLevel)
connL, connR := net.Pipe()
m := newMultiplexer(connR, zap.New(core))
if err := m.dataR.Close(); err != nil {
t.Error(err)
}
if err := m.dataL.Close(); err != nil {
t.Error(err)
}
if err := connL.SetWriteDeadline(time.Now().Add(time.Second)); err != nil {
t.Error(err)
}
if _, err := connL.Write([]byte{1, 2, 3, 4}); err != nil {
t.Error(err)
}
timeout := time.Tick(time.Second * 5)
for logs.Len() < 1 {
select {
case <-timeout:
t.Error("timed out waiting for logs")
default:
continue
}
}
})
}