-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevents_test.go
96 lines (77 loc) · 2.25 KB
/
events_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
package frame_test
import (
"context"
"fmt"
"github.com/pitabwire/frame"
"github.com/sirupsen/logrus"
"testing"
"time"
)
type MessageToTest struct {
Service *frame.Service
Count int
}
func (event *MessageToTest) Name() string {
return "message.to.test"
}
func (event *MessageToTest) PayloadType() any {
pType := ""
return &pType
}
func (event *MessageToTest) Validate(ctx context.Context, payload any) error {
if _, ok := payload.(*string); !ok {
return fmt.Errorf(" payload is %T not of type %T", payload, event.PayloadType())
}
return nil
}
func (event *MessageToTest) Execute(ctx context.Context, payload any) error {
message := payload.(*string)
logger := logrus.WithField("payload", message).WithField("type", event.Name())
logger.Info("handling event")
event.Count = event.Count + 1
return nil
}
func TestService_RegisterEventsWorks(t *testing.T) {
var cfg frame.ConfigurationDefault
err := frame.ConfigProcess("", &cfg)
if err != nil {
t.Errorf("could not processFunc configs %s", err)
return
}
events := frame.RegisterEvents(&MessageToTest{})
ctx, srv := frame.NewService("Test Srv", events, frame.Config(&cfg), frame.NoopDriver())
if srv.SubscriptionIsInitiated(cfg.EventsQueueName) {
t.Errorf("Subscription to event queue is invalid")
}
if err := srv.Run(ctx, ""); err != nil {
t.Errorf("We somehow fail to instantiate subscription ")
}
if !srv.SubscriptionIsInitiated(cfg.EventsQueueName) {
t.Errorf("Subscription to event queue is not done, should be subscribed")
}
srv.Stop(ctx)
}
func TestService_EventsPublishingWorks(t *testing.T) {
var cfg frame.ConfigurationDefault
err := frame.ConfigProcess("", &cfg)
if err != nil {
t.Errorf("could not processFunc configs %s", err)
return
}
testEvent := MessageToTest{Count: 50}
events := frame.RegisterEvents(&testEvent)
ctx, srv := frame.NewService("Test Srv", frame.Config(&cfg), frame.NoopDriver())
srv.Init(events)
if err = srv.Run(ctx, ""); err != nil {
t.Errorf("We somehow fail to instantiate subscription %s", err)
}
err = srv.Emit(ctx, testEvent.Name(), "££ yoow")
if err != nil {
t.Errorf("We failed to emit a job %s", err)
}
time.Sleep(2 * time.Second)
if testEvent.Count != 51 {
t.Errorf("Subscription event was not processed")
}
srv.Stop(ctx)
}