-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_test.go
170 lines (168 loc) · 4.57 KB
/
e2e_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
package test
import (
"context"
"github.com/awakari/client-sdk-go/api"
"github.com/awakari/client-sdk-go/model"
"github.com/awakari/core/test/config"
"github.com/awakari/core/test/data"
"github.com/cloudevents/sdk-go/binding/format/protobuf/v2/pb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/metadata"
"testing"
"time"
)
func Test_MessageDelivery(t *testing.T) {
//
cfg, err := config.NewConfigFromEnv()
require.Nil(t, err)
//
var client api.Client
client, err = api.
NewClientBuilder().
SubscriptionsUri(cfg.Uri.Subscriptions).
WriterUri(cfg.Uri.Resolver).
ReaderUri(cfg.Uri.Reader).
Build()
require.Nil(t, err)
defer client.Close()
//
groupIdCtx := metadata.AppendToOutgoingContext(context.TODO(), "x-awakari-group-id", "test-group-0")
//
var subIds []string
for _, subData := range data.Subs {
var subId string
subId, err = client.CreateSubscription(groupIdCtx, "test-user-0", subData)
require.Nil(t, err)
subIds = append(subIds, subId)
}
//
//time.Sleep(100 * time.Second) // wait for the cond/sub cache entries expiration
//
var msgsWriter model.Writer[*pb.CloudEvent]
msgsWriter, err = client.OpenMessagesWriter(groupIdCtx, "test-user-1")
require.Nil(t, err)
defer msgsWriter.Close()
var msgCount uint32
msgCount, err = msgsWriter.WriteBatch(data.Msgs)
require.Equal(t, len(data.Msgs), int(msgCount))
require.Nil(t, err)
//
cases := map[string]struct {
subId string
msgs []*pb.CloudEvent
err error
}{
"disabled": {
subId: subIds[0],
msgs: []*pb.CloudEvent{},
err: context.DeadlineExceeded,
},
"exact complete value match for a key": {
subId: subIds[1],
msgs: []*pb.CloudEvent{
data.Msgs[4],
},
},
"partial exact match": {
subId: subIds[2],
msgs: []*pb.CloudEvent{
data.Msgs[0],
},
},
"basic group condition with \"and\" logic and partial sub-conditions": {
subId: subIds[3],
msgs: []*pb.CloudEvent{
data.Msgs[1],
},
},
"basic group condition with \"or\" logic": {
subId: subIds[4],
msgs: []*pb.CloudEvent{
data.Msgs[2],
data.Msgs[3],
},
},
"basic group condition with \"and\" logic and a negative sub-condition": {
subId: subIds[5],
msgs: []*pb.CloudEvent{
data.Msgs[0],
data.Msgs[1],
},
},
"time is before - a numeric condition": {
subId: subIds[6],
msgs: []*pb.CloudEvent{
data.Msgs[1],
data.Msgs[3],
},
},
}
//
for k, c := range cases {
t.Run(k, func(t *testing.T) {
//
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Minute)
defer cancel()
var msgsReader model.Reader[[]*pb.CloudEvent]
msgsReader, err = client.OpenMessagesReader(ctx, "test-user-0", c.subId, 4)
require.Nil(t, err)
defer msgsReader.Close()
//
var msgs []*pb.CloudEvent
var msgBatch []*pb.CloudEvent
for {
msgBatch, err = msgsReader.Read()
assert.True(t, err == nil || err == context.DeadlineExceeded)
msgs = append(msgs, msgBatch...)
select {
case <-ctx.Done():
err = ctx.Err()
default:
continue
}
if err != nil {
break
}
}
assert.Equal(t, len(c.msgs), len(msgs), c.subId)
var msgFound bool
for _, msgWant := range c.msgs {
for _, msgGot := range msgs {
msgFound = msgWant.Id == msgGot.Id
if !msgFound {
continue
}
msgFound = msgWant.GetTextData() == msgGot.GetTextData()
if !msgFound {
continue
}
for attrKey, attrVal := range msgWant.Attributes {
switch attrValT := attrVal.Attr.(type) {
case *pb.CloudEventAttributeValue_CeBoolean:
msgFound = attrValT.CeBoolean == msgGot.Attributes[attrKey].GetCeBoolean()
case *pb.CloudEventAttributeValue_CeInteger:
msgFound = attrValT.CeInteger == msgGot.Attributes[attrKey].GetCeInteger()
case *pb.CloudEventAttributeValue_CeString:
msgFound = attrValT.CeString == msgGot.Attributes[attrKey].GetCeString()
case *pb.CloudEventAttributeValue_CeUri:
msgFound = attrValT.CeUri == msgGot.Attributes[attrKey].GetCeUri()
case *pb.CloudEventAttributeValue_CeUriRef:
msgFound = attrValT.CeUriRef == msgGot.Attributes[attrKey].GetCeUriRef()
case *pb.CloudEventAttributeValue_CeTimestamp:
msgFound = attrValT.CeTimestamp.AsTime() == msgGot.Attributes[attrKey].GetCeTimestamp().AsTime()
}
// no need to check other attributes if any not equals
if !msgFound {
break
}
}
if msgFound {
break
}
}
assert.Truef(t, msgFound, "msg id = %s was not found among the received messages: %+v", msgWant.Id, msgs)
}
})
}
}