forked from nats-io/nats.go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.go
More file actions
91 lines (75 loc) · 1.93 KB
/
json_test.go
File metadata and controls
91 lines (75 loc) · 1.93 KB
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
// Copyright 2012 Apcera Inc. All rights reserved.
package nats
import (
"reflect"
"testing"
)
func NewJsonEncodedConn(t *testing.T) *EncodedConn {
ec, err := NewEncodedConn(newConnection(t), "json")
if err != nil {
t.Fatalf("Failed to create an encoded connection: %v\n", err)
}
return ec
}
func TestJsonMarshalString(t *testing.T) {
ec := NewJsonEncodedConn(t)
defer ec.Close()
ch := make(chan bool)
testString := "Hello World!"
ec.Subscribe("json_string", func(s string) {
if s != testString {
t.Fatalf("Received test string of '%s', wanted '%s'\n", s, testString)
}
ch <- true
})
ec.Publish("json_string", testString)
if e := wait(ch); e != nil {
t.Fatal("Did not receive the message")
}
}
func TestJsonMarshalInt(t *testing.T) {
ec := NewJsonEncodedConn(t)
defer ec.Close()
ch := make(chan bool)
testN := 22
ec.Subscribe("json_int", func(n int) {
if n != testN {
t.Fatalf("Received test int of '%d', wanted '%d'\n", n, testN)
}
ch <- true
})
ec.Publish("json_int", testN)
if e := wait(ch); e != nil {
t.Fatal("Did not receive the message")
}
}
type person struct {
Name string
Address string
Age int
Children map[string]*person
Assets map[string]uint
}
func TestJsonMarshalStruct(t *testing.T) {
ec := NewJsonEncodedConn(t)
defer ec.Close()
ch := make(chan bool)
me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
me.Children = make(map[string]*person)
me.Children["sam"] = &person{Name: "sam", Age: 16, Address: "85 Second St"}
me.Children["meg"] = &person{Name: "meg", Age: 14, Address: "85 Second St"}
me.Assets = make(map[string]uint)
me.Assets["house"] = 1000
me.Assets["car"] = 100
ec.Subscribe("json_struct", func(p *person) {
ch <- true
if !reflect.DeepEqual(p, me) {
t.Fatalf("Did not receive the correct struct response")
}
ch <- true
})
ec.Publish("json_struct", me)
if e := wait(ch); e != nil {
t.Fatal("Did not receive the message")
}
}