-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmessages_test.go
73 lines (69 loc) · 1.8 KB
/
messages_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
// Copyright (c) 2020 Meng Huang (mhboy@outlook.com)
// This package is licensed under a MIT license that can be found in the LICENSE file.
package socket
import (
"io"
"os"
"strings"
"testing"
)
func TestMessages(t *testing.T) {
name := "tmpTestMessages"
file, _ := os.Create(name)
messages := NewMessages(file, true)
str := strings.Repeat("Hello World", 50)
err := messages.WriteMessage([]byte(str))
if err != nil {
t.Log(err)
}
file.Seek(0, os.SEEK_SET)
messages.(BufferedOutput).SetBufferedOutput(0)
messages.(BufferedOutput).SetBufferedOutput(64)
messages.(BufferedOutput).SetBufferedOutput(64)
messages.(BufferedInput).SetBufferedInput(0)
messages.(BufferedInput).SetBufferedInput(2)
if msg, err := messages.ReadMessage(make([]byte, 65536)); err != nil {
t.Error(err)
} else if string(msg) != str {
t.Error(string(msg))
}
if _, err := messages.ReadMessage(nil); err != io.EOF {
t.Error(err)
}
messages.Close()
messages.Close()
os.Remove(name)
}
func TestMessagesRetain(t *testing.T) {
name := "tmpTestMessages"
file, _ := os.Create(name)
defer os.Remove(name)
writeBufferSize := 7
readBufferSize := 7
var readBuffer []byte
var writeBuffer []byte
readBuffer = make([]byte, readBufferSize)
writeBuffer = make([]byte, writeBufferSize)
messages := &messages{
shared: false,
reader: file,
writer: file,
closer: file,
writeBufferSize: writeBufferSize,
readBufferSize: readBufferSize,
readBuffer: readBuffer,
writeBuffer: writeBuffer,
}
str := strings.Repeat("Hello World", 50)
err := messages.WriteMessage([]byte(str))
if err != nil {
t.Log(err)
}
file.Seek(0, os.SEEK_SET)
if msg, err := messages.ReadMessage(nil); err != nil {
t.Error(err)
} else if string(msg) != str {
t.Error(string(msg))
}
messages.Close()
}