-
Notifications
You must be signed in to change notification settings - Fork 3
/
scenario_test.go
116 lines (106 loc) · 2.36 KB
/
scenario_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
// Copyright 2019 Koninklijke KPN N.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"reflect"
"strings"
"testing"
"time"
)
func TestNewScenarioFromReader(t *testing.T) {
j := `
[
{
"t":15,
"subscribe":[],
"publish":[{"topic":"t","payload":"Y29vbAo="}]
},
{
"t":11,
"subscribe":["a"]
},
{
"t":17,
"subscribe":["a"],
"disconnect":true
}
]
`
exp := []Step{
{T: 11, Subscribe: &[]string{"a"}, Wait: time.Second * 4},
{T: 15, Subscribe: &[]string{}, Publish: []Publication{{Topic: "t", Payload: []byte("cool\x0a")}}, Wait: time.Second * 2},
{T: 17, Subscribe: &[]string{"a"}, Disconnect: true, Wait: time.Second * 5},
}
steps, err := newScenarioFromReader(strings.NewReader(j))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(exp, steps) {
t.Fail()
}
// invalid json
if _, err := newScenarioFromReader(strings.NewReader("so much fun")); err == nil {
t.Fail()
}
}
func TestInfiniteRandomScenarioChans(t *testing.T) {
steps := []Step{
{Subscribe: &[]string{"a"}, Wait: time.Nanosecond},
{Subscribe: &[]string{}, Publish: []Publication{{Topic: "t", Payload: []byte("cool\x0a")}}, Wait: time.Nanosecond},
{Subscribe: &[]string{"b"}, Disconnect: true, Wait: time.Nanosecond},
}
irs := NewInfiniteRandomScenario(context.TODO(), steps)
irs.randFunc = func(int) int { return 0 }
sub, pub, disc := irs.Chans()
// step0
s := <-sub
if s[0] != "a" {
t.Fail()
}
// step1
var p Publication
select {
case pp := <-pub:
p = pp
case ss := <-sub:
s = ss
}
select {
case pp := <-pub:
p = pp
case ss := <-sub:
s = ss
}
if p.Topic != "t" || string(p.Payload) != "cool\x0a" {
t.Fail()
}
if len(s) != 0 {
t.Fail()
}
// step2
select {
case ss := <-sub:
s = ss
case <-disc:
}
select {
case ss := <-sub:
s = ss
case <-disc:
}
if s[0] != "b" {
t.Fail()
}
}