-
Notifications
You must be signed in to change notification settings - Fork 10
/
client_test.go
75 lines (66 loc) · 1.88 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
package ucp
import (
"bufio"
"bytes"
"fmt"
"golang.org/x/time/rate"
"log"
"os"
"reflect"
"sync"
"testing"
)
func TestInitRefNum(t *testing.T) {
client := &Client{
mu: &sync.Mutex{},
logger: log.New(os.Stdout, "debug ", 0),
}
client.initRefNum()
firstRefNum := client.nextRefNum()
expectedFirstRefNum := []byte("00")
if !bytes.Equal(firstRefNum, expectedFirstRefNum) {
t.Errorf("Expected %v got %v\n", expectedFirstRefNum, firstRefNum)
}
// advance to "99"
for i := 1; i <= 99; i++ {
client.nextRefNum()
}
// should reset to "00"
firstRefNum = client.nextRefNum()
if !bytes.Equal(firstRefNum, expectedFirstRefNum) {
t.Errorf("Expected %v got %v\n", expectedFirstRefNum, firstRefNum)
}
}
func TestSend(t *testing.T) {
buf := new(bytes.Buffer)
submitSmRespCh := make(chan []string, 1)
client := &Client{
mu: &sync.Mutex{},
muconn: &sync.Mutex{},
logger: log.New(os.Stdout, "debug ", 0),
rateLimiter: rate.NewLimiter(rate.Limit(1), 1),
writer: bufio.NewWriter(buf),
submitSmRespCh: submitSmRespCh,
}
ack := []string{"01", "00044", "R", "51", "A", "", "09191234567:110917173639", "95"}
client.initRefNum()
select {
case submitSmRespCh <- ack:
fmt.Println("sent ack")
default:
fmt.Println("cant send ack")
}
ids, err := client.Send("test", "09191234567", "hello world")
expectedIds := []string{"09191234567:110917173639"}
expectedBytesWritten := []byte("\x0200/00120/O/51/09191234567/08F4F29C0E//1//1/////////////3/88/68656C6C6F20776F726C64////1////5039//020100060101070101///B7\x03")
actualBytesWritten := buf.Bytes()
if !bytes.Equal(expectedBytesWritten, actualBytesWritten) {
t.Errorf("Expected %v got %v\n", expectedBytesWritten, actualBytesWritten)
}
if err != nil {
t.Error("Expected nil error\n")
}
if !reflect.DeepEqual(expectedIds, ids) {
t.Errorf("Expected %v got %v\n", expectedIds, ids)
}
}