-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_tcp_test.go
62 lines (53 loc) · 1.47 KB
/
server_tcp_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
package main
import (
"log"
"net"
"net/url"
"strconv"
"strings"
"testing"
"time"
)
func TestLoogutServer(t *testing.T) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
logout := NewLogoutServer()
// Setup a test pipe.
server, client := net.Pipe()
go logout.handleTcpRequest(server, false)
// Check to make sure the server sends back the URI.
uri := readLine(client)
token, err := parseToken(uri)
checkEq(t, err, nil)
// Sanity check internal structures.
checkEq(t, len(logout.manager.cache), 1)
ls, exists := logout.manager.cache[token]
checkEq(t, exists, true)
checkEq(t, ls.rb.Len(), 0)
// Write some logs on the client.
client.Write([]byte("Foo"))
client.Write([]byte("Bar"))
// Validate logs on the server.
time.Sleep(100 * time.Millisecond)
checkEq(t, ls.rb.Len(), 2)
checkEq(t, ls.rb.arr[0], []byte("Foo"))
checkEq(t, ls.rb.arr[1], []byte("Bar"))
// Close the client connection and validate server state.
client.Close()
time.Sleep(100 * time.Millisecond)
_, exists = logout.manager.cache[token]
checkEq(t, exists, false)
checkEq(t, len(logout.manager.cache), 0)
}
func readLine(conn net.Conn) string {
buf := make([]byte, 1024*1024)
readLen, _ := conn.Read(buf)
return strings.TrimSuffix(string(buf[:readLen]), "\n")
}
func parseToken(uri string) (logKey, error) {
u, err := url.Parse(uri)
if err != nil {
return logKey{}, err
}
n, err := strconv.ParseInt(u.Query()["token"][0], 16, 64)
return logKey{owner: ANONYMOUS_OWNER_ID, token: n}, err
}