-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathocpp_test.go
199 lines (174 loc) · 5.86 KB
/
ocpp_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package ocpp_test
import (
"context"
"errors"
"fmt"
"math/rand"
"strconv"
"testing"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/voltbras/go-ocpp"
"github.com/voltbras/go-ocpp/cp"
"github.com/voltbras/go-ocpp/cs"
"github.com/voltbras/go-ocpp/messages/v1x/cpreq"
"github.com/voltbras/go-ocpp/messages/v1x/cpresp"
"github.com/voltbras/go-ocpp/messages/v1x/csreq"
"github.com/voltbras/go-ocpp/messages/v1x/csresp"
)
func Test_Connection(t *testing.T) {
cpointConnected := make(chan string)
cpointDisconnected := make(chan string)
csysPort := ":5050"
csys := cs.New()
csys.SetChargePointConnectionListener(func(cpID string) {
// t.Log("cpoint connected: ", cpID)
cpointConnected <- cpID
})
csys.SetChargePointDisconnectionListener(func(cpID string) {
// t.Log("cpoint disconnected: ", cpID)
cpointDisconnected <- cpID
})
go csys.Run(csysPort, func(req cpreq.ChargePointRequest, metadata cs.ChargePointRequestMetadata) (cpresp.ChargePointResponse, error) {
switch req.(type) {
case *cpreq.Heartbeat:
return &cpresp.Heartbeat{}, nil
}
return nil, errors.New("not supported")
})
csysURL := "ws://localhost" + csysPort
t.Run("one chargepoint", func(t *testing.T) {
cpID := "123"
t.Run("single time", func(t *testing.T) {
_, disconnect := testConnectionDisconnection(t, cpID, csysURL, cpointConnected, cpointDisconnected)
disconnect()
})
t.Run("multiple times", func(t *testing.T) {
attempts := 100
for i := 0; i <= attempts; i++ {
_, disconnect := testConnectionDisconnection(t, cpID, csysURL, cpointConnected, cpointDisconnected)
disconnect()
}
})
})
t.Run("multiple chargepoints", func(t *testing.T) {
t.Run("shuffled connect + disconnect", func(t *testing.T) {
a := "chargerA"
b := "chargerB"
c := "chargerC"
_, disconnectA := testConnectionDisconnection(t, a, csysURL, cpointConnected, cpointDisconnected)
_, disconnectB := testConnectionDisconnection(t, b, csysURL, cpointConnected, cpointDisconnected)
_, disconnectC := testConnectionDisconnection(t, c, csysURL, cpointConnected, cpointDisconnected)
disconnectB()
disconnectA()
disconnectC()
})
t.Run("large chargers pool, shuffled connect + disconnect", func(t *testing.T) {
chargerPoolSize := 100
chargerPool := make([]struct {
id string
disconnect func()
}, chargerPoolSize)
// creating and connecting chargers
for i := 0; i < chargerPoolSize; i++ {
cpID := strconv.Itoa(i)
_, disconnect := testConnectionDisconnection(t, cpID, csysURL, cpointConnected, cpointDisconnected)
chargerPool[i] = struct {
id string
disconnect func()
}{
id: cpID,
disconnect: disconnect,
}
}
rand.Shuffle(chargerPoolSize, func(i, j int) {
chargerPool[i], chargerPool[j] = chargerPool[j], chargerPool[i]
})
for _, charger := range chargerPool {
// t.Log(i, charger.id)
charger.disconnect()
}
})
})
shouldSendCommandToChargePoint := func(cpID string) {
svc, err := csys.GetServiceOf(cpID, ocpp.V16, "")
assert.NoError(t, err)
_, err = svc.Send(&csreq.GetConfiguration{})
assert.NoError(t, err)
}
shouldNotSendCommandToChargePoint := func(cpID string) {
svc, err := csys.GetServiceOf(cpID, ocpp.V16, "")
if svc == nil {
assert.Error(t, err)
return
}
_, err = svc.Send(&csreq.GetConfiguration{})
assert.Error(t, err)
}
t.Run("double connect before disconnecting", func(t *testing.T) {
_, disconnectA := testConnectionDisconnection(t, "123", csysURL, cpointConnected, cpointDisconnected)
_, disconnectB := testConnectionDisconnection(t, "123", csysURL, cpointConnected, cpointDisconnected)
shouldSendCommandToChargePoint("123")
disconnectA()
shouldSendCommandToChargePoint("123")
disconnectB()
shouldNotSendCommandToChargePoint("123")
})
// this test is still not passing every time
// it seems that we have a data race somewhere
t.Run("anomalous connection", func(t *testing.T) {
cpoint, _ := testConnectionDisconnection(t, "123", csysURL, cpointConnected, cpointDisconnected)
shouldSendCommandToChargePoint("123")
// shouldn't close on invalid message
err := cpoint.Connection().WriteMessage(websocket.TextMessage, []byte("[ab"))
assert.NoError(t, err)
shouldSendCommandToChargePoint("123")
for i := 0; i < 1000; i++ {
// BEGIN
cpoint.Connection().Close()
<-cpoint.WaitDisconnect()
<-csys.WaitDisconnect("123")
shouldNotSendCommandToChargePoint("123")
<-cpoint.WaitConnect()
<-csys.WaitConnect("123")
shouldSendCommandToChargePoint("123")
}
})
}
func testConnectionDisconnection(t *testing.T, cpID, csysURL string, cpointConnected, cpointDisconnected chan string) (cpoint cp.ChargePoint, disconnect func()) {
cpctx, killCp := context.WithCancel(context.Background())
cpoint, err := cp.New(cpctx, cpID, csysURL, ocpp.V16, ocpp.JSON, nil, func(req csreq.CentralSystemRequest) (csresp.CentralSystemResponse, error) {
switch req.(type) {
case *csreq.GetConfiguration:
return &csresp.GetConfiguration{}, nil
}
return nil, errors.New("not supported")
})
if err != nil {
t.Fatal(fmt.Errorf("chargepoint could not start: %w", err))
}
connectedCpID := <-cpointConnected
if cpoint.Identity() != connectedCpID {
// t.Log("correct charge point did not connect")
cpointConnected <- connectedCpID
}
_, err = cpoint.Send(&cpreq.Heartbeat{})
assert.NoError(t, err)
done := make(chan struct{})
go func() {
for disconnectedCpID := range cpointDisconnected {
if cpoint.Identity() != disconnectedCpID {
// t.Logf("correct charge point did not disconnect. charge point expected (%s), charge point disconnected(%s)", cpoint.Identity(), disconnectedCpID)
cpointDisconnected <- disconnectedCpID
} else {
// t.Log("correctly disconnected charge point: " + cpoint.Identity())
done <- struct{}{}
return
}
}
}()
return cpoint, func() {
killCp()
<-done
}
}