/
sift_test.go
183 lines (158 loc) · 5.03 KB
/
sift_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package sift_test
import (
"fmt"
"github.com/upwrd/sift"
"github.com/upwrd/sift/adapter/example"
"github.com/upwrd/sift/db"
"github.com/upwrd/sift/notif"
"github.com/upwrd/sift/types"
. "gopkg.in/check.v1"
"net"
"strconv"
"testing"
"time"
)
// Hook up gocheck into the "go test" runner.
func TestSIFT(t *testing.T) { TestingT(t) }
const (
testSiftSimplePort = uint16(10952)
testGetComponentsPort = uint16(10953)
testSiftPort = uint16(10954)
)
type SiftSuite struct{}
var _ = Suite(&SiftSuite{})
func (s *SiftSuite) TestServerNetwork(c *C) {
if testing.Short() {
c.Skip("Test uses local network, skipping in short mode")
}
// SetLogLevel("debug")
// Start an example service server
exServ := example.NewServer(testSiftPort)
go exServ.Serve()
defer exServ.Stop()
<-time.After(500 * time.Millisecond)
// Confirm that the example server is reachable
url := "localhost:" + strconv.Itoa(int(testSiftPort))
conn, err := net.DialTimeout("tcp", url, time.Second)
c.Assert(err, IsNil)
conn.Close()
// start a new SIFT server
siftServ, err := sift.NewServer("")
c.Assert(err, IsNil)
c.Assert(siftServ, NotNil)
defer func() {
err = siftServ.StopAndWait(1 * time.Minute)
c.Assert(err, IsNil)
}()
go siftServ.Serve()
components, err := siftServ.GetComponents(db.ExpandNone)
c.Assert(err, IsNil)
c.Assert(len(components), Equals, 0, Commentf("components: %+v", components))
af := example.NewFactory(testSiftPort)
_, err = siftServ.AddAdapterFactory(af)
c.Assert(err, IsNil)
// create a listener for component updates
token := siftServ.Login()
listener := siftServ.Listen(token, notif.ComponentFilter{}) // listen to components
c.Assert(listener, NotNil)
//
// example --> SIFT
//
// Add a light to the example service
light1 := example.Light{
IsOn: true,
OutputInPercent: 100,
}
device1 := example.Device{
Components: map[string]example.Component{"light1": light1},
}
exServ.SetDevice("device1", device1) // This should produce an update
// Confirm that the SIFT server receives an update for the new light
timeout := time.After(20 * time.Second)
select {
case <-listener:
case <-timeout:
c.Fatalf("SIFT timed out waiting for update from example server")
}
// The SIFT server should have one component, the LightEmitter
components, err = siftServ.GetComponents(db.ExpandNone)
c.Assert(err, IsNil)
c.Assert(len(components), Equals, 1)
var componentID types.ComponentID
var component types.Component
for key, comp := range components {
component = comp
componentID = key
}
c.Assert(componentID.Name, Not(Equals), "")
c.Assert(componentID.DeviceID, Not(Equals), 0)
expected := types.LightEmitter{
BaseComponent: types.BaseComponent{
Make: "example",
Model: "light_emitter_1",
},
State: types.LightEmitterState{
BrightnessInPercent: 100,
},
}
c.Assert(component, Equals, expected)
//
// SIFT --> example
//
// Send an intent to change the brightness of the light
setBrightness := types.SetLightEmitterIntent{
BrightnessInPercent: 42,
}
err = siftServ.EnactIntent(componentID, setBrightness)
c.Assert(err, IsNil)
timeout = time.After(20 * time.Second)
select {
case <-listener:
case <-timeout:
c.Fatalf("SIFT timed out waiting for update from example server")
}
components, err = siftServ.GetComponents(db.ExpandNone)
c.Assert(err, IsNil)
component, ok := components[componentID]
c.Assert(ok, Equals, true)
expected.State.BrightnessInPercent = 42 // updated expected to reflect server-side change
c.Assert(component, Equals, expected)
// err = exServ.close()
// c.Assert(err, IsNil)
}
func Example() {
// start a new SIFT server
serv, _ := sift.NewServer("") // "" indicates a random, temporary file
go serv.Serve()
defer func() {
serv.StopAndWait(1 * time.Minute)
}()
// With no running adapters, the SIFT server should not report any connected components
components, _ := serv.GetComponents(db.ExpandAll)
fmt.Printf("len(components) == %v", len(components)) // "len(components) == 0"
// Start an example service server running on localhost port 12345. This is
// a demonstration server that you can control programatically for testing.
exServ := example.NewServer(uint16(12345))
go exServ.Serve()
defer exServ.Stop()
<-time.After(500 * time.Millisecond)
// Add a light to the example service. This code is specific to the example
// server. In a real-world application, a light would be added when it is
// plugged in and connected as defined by the manufacturer.
light1 := example.Light{
IsOn: true,
OutputInPercent: 100,
}
device1 := example.Device{
Components: map[string]example.Component{"light1": light1},
}
exServ.SetDevice("device1", device1) // This should produce an update
// Add an Adapter Factory to monitor the example service
serv.AddAdapterFactory(example.NewFactory(12345))
components, _ = serv.GetComponents(db.ExpandAll)
fmt.Printf("len(components) == %v", len(components)) // "len(components) == 1"
fmt.Printf("components:")
for id, component := range components {
fmt.Printf(" component %v: %+v", id, component)
}
}