-
Notifications
You must be signed in to change notification settings - Fork 8
/
testclient.go
269 lines (238 loc) · 6.29 KB
/
testclient.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package testclient
import (
"fmt"
"github.com/DavidAFox/gentest"
"math/rand"
)
//Client is an interface for using the ChatTest including all the functions that it calls
type Client interface {
Login()
Block(string)
UnBlock(string)
Who(string)
List()
Join(string)
Send(string)
CheckResponse()
Name() string
Update()
}
//SharedClient is an interface for use in the shared ClientData
type SharedClient interface {
Update()
CheckResponse()
}
//join is an actor to do the clients join function.
type join struct {
cl Client
}
func (j *join) Do(shared interface{}) string {
var data *ClientData
var ok bool
if data, ok = shared.(*ClientData); !ok {
panic("join shared not ClientData")
}
room := randomName(data.Rooms)
j.cl.Join(room)
return fmt.Sprintf("Joined %v", room)
}
//send is an actor to do the clients send function.
type send struct {
cl Client
}
func (s *send) Do(shared interface{}) string {
message := RandomString(25)
s.cl.Send(message)
return fmt.Sprintf("Sent %v", message)
}
//block is an actor to do the clients block function.
type block struct {
cl Client
}
func (b *block) Do(shared interface{}) string {
var data *ClientData
var ok bool
if data, ok = shared.(*ClientData); !ok {
panic("block shared not ClientData")
}
name := randomName(data.Names)
b.cl.Block(name)
return fmt.Sprintf("blocked %v", name)
}
//unblock is an actor to do the clients unblock function.
type unblock struct {
cl Client
}
func (u *unblock) Do(shared interface{}) string {
var data *ClientData
var ok bool
if data, ok = shared.(*ClientData); !ok {
panic("unblock shared not ClientData")
}
name := randomName(data.Names)
u.cl.UnBlock(name)
return fmt.Sprintf("unblocked %v", name)
}
//list is an actor to do the clients list function.
type list struct {
cl Client
}
func (l *list) Do(shared interface{}) string {
l.cl.List()
return fmt.Sprintf("list")
}
//who is an actor to do the clients who function.
type who struct {
cl Client
}
func (w *who) Do(shared interface{}) string {
var data *ClientData
var ok bool
if data, ok = shared.(*ClientData); !ok {
panic("who shared not ClientData")
}
room := randomName(data.Rooms)
w.cl.Who(room)
return fmt.Sprintf("who %v", room)
}
//NewActionList creates a slice of the clients actors.
func NewActionList(cl Client) []gentest.Actor {
alist := make([]gentest.Actor, 6, 6)
join := new(join)
join.cl = cl
alist[0] = join
send := new(send)
send.cl = cl
alist[1] = send
block := new(block)
block.cl = cl
alist[2] = block
unblock := new(unblock)
unblock.cl = cl
alist[3] = unblock
list := new(list)
list.cl = cl
alist[4] = list
who := new(who)
who.cl = cl
alist[5] = who
return alist
}
//restsend is an actor for a rest client's send function.
type restsend struct {
cl *RestClient
}
func (rs *restsend) Do(shared interface{}) string {
var data *ClientData
var ok bool
if data, ok = shared.(*ClientData); !ok {
panic("restsend shared not ClientData")
}
room := randomName(data.Rooms)
msg := RandomString(25)
rs.cl.Send(msg, room)
return fmt.Sprintf("restsend %v to %v", msg, room)
}
//restget is an actor for a rest client's get function.
type restget struct {
cl *RestClient
}
func (rg *restget) Do(shared interface{}) string {
var data *ClientData
var ok bool
if data, ok = shared.(*ClientData); !ok {
panic("restget shared not ClientData")
}
room := randomName(data.Rooms)
rg.cl.Get(room)
return fmt.Sprintf("restget from %v", room)
}
//NewRestActionList creates a slice of rest client actors.
func NewRestActionList(cl *RestClient) []gentest.Actor {
alist := make([]gentest.Actor, 2, 2)
restsend := new(restsend)
restsend.cl = cl
alist[0] = restsend
restget := new(restget)
restget.cl = cl
alist[1] = restget
return alist
}
//TestClient can hold either a regular client or a rest client. The client that it uses is determined by its action.
type TestClient struct {
client Client
rest *RestClient
action *gentest.Test
name string
}
//ClientData is an object for passing the necessary data into the actions.
type ClientData struct {
Rooms []string
Names []string
Clients []SharedClient
}
//NewTestClient returns a test client with default equal weights for its actions.
func NewTestClient(cl Client) *TestClient {
tcl := new(TestClient)
tcl.client = cl
tcl.name = cl.Name()
tcl.action = gentest.New(cl.Name(), NewActionList(cl), cl)
return tcl
}
//NewTestRestClient returns a test client that uses the rest inteface with default weights for its actions.
func NewTestRestClient(cl *RestClient) *TestClient {
tcl := new(TestClient)
tcl.rest = cl
tcl.name = cl.Name()
tcl.action = gentest.New(cl.Name(), NewRestActionList(cl), cl)
return tcl
}
//NewTestClientWithWeights returns a test client with weighted action probabilites. The weights slice should contain int for the weights in the order of join, send, block, unblock, list, who.
func NewTestClientWithWeights(cl Client, weights []int) *TestClient {
tcl := new(TestClient)
tcl.client = cl
tcl.name = cl.Name()
tcl.action = gentest.NewWithWeights(cl.Name(), NewActionList(cl), cl, weights)
return tcl
}
//Client() returns the TestClient's Client.
func (tc *TestClient) Client() SharedClient {
if tc.client != nil {
return tc.client
}
if tc.rest != nil {
return tc.rest
}
return nil
}
//Do is a method that allows the test client to meet the Actor interface. It will run a random client action from its action list.
func (tc *TestClient) Do(shared interface{}) string {
str := tc.action.Do(shared)
update(shared)
return fmt.Sprintf("%v", str)
}
//update takes a ClientData in interface{} form and runs Update() on each of the cleitns in the client list.
func update(shared interface{}) {
var data *ClientData
var ok bool
if data, ok = shared.(*ClientData); !ok {
panic("shared not ClientData")
}
for i := range data.Clients {
data.Clients[i].Update()
}
}
//RandomString returns a random string of maxlen > len > 0 made up of upper and lower case letters and/or numbers.
func RandomString(maxlen int) string {
x := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
l := rand.Int()%maxlen + 1
var s string
for i := 0; i < l; i++ {
s = s + string(x[rand.Int()%len(x)])
}
return s
}
//randomName is a helper function that returns a random room name from rml.
func randomName(nl []string) string {
return nl[rand.Int()%len(nl)]
}