-
Notifications
You must be signed in to change notification settings - Fork 6
/
xport_tcp_test.go
132 lines (109 loc) · 2.8 KB
/
xport_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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// February 2016, cisco
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
//
package main
import (
"bytes"
"encoding/binary"
samples "github.com/cisco-ie/pipeline-gnmi/mdt_msg_samples"
"net"
"testing"
"time"
)
func TestTCPServerStart(t *testing.T) {
var dataChans = make([]chan<- dataMsg, 0)
ctrlChan := make(chan *ctrlMsg)
ctrlChan1 := make(chan *ctrlMsg)
ctrlChan2 := make(chan *ctrlMsg)
dataChan := make(chan dataMsg, DATACHANNELDEPTH)
dataChans = append(dataChans, dataChan)
err := addTCPServer("TestTCP", ":5556", "st", dataChans, ctrlChan, 0, true)
if err != nil {
t.Errorf("setup function fail to startup TCP server")
return
}
err = addTCPServer("TestTCP", ":5556", "st", dataChans, ctrlChan1, 1000, true)
//
// NEGATIVE: This should fail because we are already bound to the same port
if err == nil {
t.Errorf("setup function succeded to startup TCP server, but expected fail")
return
}
err = addTCPServer("TestTCP2", ":5559", "st", dataChans, ctrlChan2, 1000, true)
if err != nil {
t.Errorf("setup function fail to startup second TCP server (port REALLY in use?)")
return
}
time.Sleep(1 * time.Second)
//
// Bring up connection and test sending content
conn, err := net.Dial("tcp", ":5556")
if err != nil {
t.Errorf("Failed to connect to server")
return
}
sample := samples.MDTSampleTelemetryTableFetchOne(
samples.SAMPLE_TELEMETRY_DATABASE_BASIC)
if sample == nil {
t.Errorf("Failed to fetch data")
return
}
fullmsg := sample.SampleStreamGPB
hdr := encapSTHdr{
MsgType: ENC_ST_HDR_MSG_TYPE_TELEMETRY_DATA,
MsgEncap: ENC_ST_HDR_MSG_ENCAP_GPB,
MsgHdrVersion: ENC_ST_HDR_VERSION,
Msgflag: ENC_ST_HDR_MSG_FLAGS_NONE,
Msglen: uint32(len(fullmsg)),
}
err = binary.Write(conn, binary.BigEndian, &hdr)
if err != nil {
t.Errorf("Failed to write data header")
return
}
wrote, err := conn.Write(fullmsg)
if err != nil {
t.Errorf("Failed write data 1")
return
}
if wrote != len(fullmsg) {
t.Errorf("Wrote %d, expect %d for data 1",
wrote, len(fullmsg))
return
}
data := <-dataChan
err, b := data.produceByteStream(dataMsgStreamSpecDefault)
if err != nil {
t.Errorf("Data failed to produce byte stream as expected")
}
if !bytes.Contains(b, fullmsg) {
t.Errorf("Failed to receive expected data")
}
//
// Test shutdown
respChan := make(chan *ctrlMsg)
request := &ctrlMsg{
id: SHUTDOWN,
respChan: respChan,
}
//
// Send shutdown message
ctrlChan <- request
// Wait for ACK
ack := <-respChan
if ack.id != ACK {
t.Error("failed to recieve acknowledgement indicating shutdown complete")
}
//
// Send shutdown message
ctrlChan2 <- request
// Wait for ACK
ack = <-respChan
if ack.id != ACK {
t.Error("failed to recieve acknowledgement indicating shutdown complete")
}
}