summaryrefslogtreecommitdiff
path: root/tests/plugins/temporal/server_test.go
blob: c8d815c37ebca6711305ebe73902348f725004c4 (plain)
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
package tests

import (
	"context"
	"testing"

	endure "github.com/spiral/endure/pkg/container"
	"github.com/spiral/roadrunner/v2/plugins/config"
	"github.com/spiral/roadrunner/v2/plugins/informer"
	"github.com/spiral/roadrunner/v2/plugins/logger"
	"github.com/spiral/roadrunner/v2/plugins/resetter"
	"github.com/spiral/roadrunner/v2/plugins/rpc"
	"github.com/spiral/roadrunner/v2/plugins/server"
	"github.com/spiral/roadrunner/v2/plugins/temporal/activity"
	rrClient "github.com/spiral/roadrunner/v2/plugins/temporal/client"
	"github.com/spiral/roadrunner/v2/plugins/temporal/workflow"
	"go.temporal.io/api/enums/v1"
	"go.temporal.io/api/history/v1"
	temporalClient "go.temporal.io/sdk/client"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

type TestServer struct {
	container  endure.Container
	temporal   rrClient.Temporal
	activities *activity.Plugin
	workflows  *workflow.Plugin
}

type ConfigOption struct {
	Name  string
	Value interface{}
}

func NewOption(name string, value interface{}) ConfigOption {
	return ConfigOption{Name: name, Value: value}
}

func NewTestServer(opt ...ConfigOption) *TestServer {
	e, err := endure.NewContainer(initLogger(), endure.RetryOnFail(false))
	if err != nil {
		panic(err)
	}

	t := &rrClient.Plugin{}
	a := &activity.Plugin{}
	w := &workflow.Plugin{}

	if err := e.Register(initConfig(opt...)); err != nil {
		panic(err)
	}

	if err := e.Register(&logger.ZapLogger{}); err != nil {
		panic(err)
	}
	if err := e.Register(&resetter.Plugin{}); err != nil {
		panic(err)
	}
	if err := e.Register(&informer.Plugin{}); err != nil {
		panic(err)
	}
	if err := e.Register(&server.Plugin{}); err != nil {
		panic(err)
	}
	if err := e.Register(&rpc.Plugin{}); err != nil {
		panic(err)
	}

	if err := e.Register(t); err != nil {
		panic(err)
	}
	if err := e.Register(a); err != nil {
		panic(err)
	}
	if err := e.Register(w); err != nil {
		panic(err)
	}

	if err := e.Init(); err != nil {
		panic(err)
	}

	errCh, err := e.Serve()
	if err != nil {
		panic(err)
	}

	go func() {
		err := <-errCh
		er := e.Stop()
		if er != nil {
			panic(err)
		}
	}()

	return &TestServer{container: e, temporal: t, activities: a, workflows: w}
}

func (s *TestServer) Client() temporalClient.Client {
	return s.temporal.GetClient()
}

func (s *TestServer) MustClose() {
	err := s.container.Stop()
	if err != nil {
		panic(err)
	}
}

func initConfig(opt ...ConfigOption) config.Configurer {
	cfg := &config.Viper{}
	cfg.Path = ".rr.yaml"
	cfg.Prefix = "rr"

	return cfg
}

func initLogger() *zap.Logger {
	cfg := zap.Config{
		Level:    zap.NewAtomicLevelAt(zap.ErrorLevel),
		Encoding: "console",
		EncoderConfig: zapcore.EncoderConfig{
			MessageKey:    "message",
			LevelKey:      "level",
			TimeKey:       "time",
			CallerKey:     "caller",
			NameKey:       "name",
			StacktraceKey: "stack",
			EncodeLevel:   zapcore.CapitalLevelEncoder,
			EncodeTime:    zapcore.ISO8601TimeEncoder,
			EncodeCaller:  zapcore.ShortCallerEncoder,
		},
		OutputPaths:      []string{"stderr"},
		ErrorOutputPaths: []string{"stderr"},
	}

	l, err := cfg.Build(zap.AddCaller())
	if err != nil {
		panic(err)
	}

	return l
}

func (s *TestServer) AssertContainsEvent(t *testing.T, w temporalClient.WorkflowRun, assert func(*history.HistoryEvent) bool) {
	i := s.Client().GetWorkflowHistory(
		context.Background(),
		w.GetID(),
		w.GetRunID(),
		false,
		enums.HISTORY_EVENT_FILTER_TYPE_ALL_EVENT,
	)

	for {
		if !i.HasNext() {
			t.Error("no more events and no match found")
			break
		}

		e, err := i.Next()
		if err != nil {
			t.Error("unable to read history event")
			break
		}

		if assert(e) {
			break
		}
	}
}

func (s *TestServer) AssertNotContainsEvent(t *testing.T, w temporalClient.WorkflowRun, assert func(*history.HistoryEvent) bool) {
	i := s.Client().GetWorkflowHistory(
		context.Background(),
		w.GetID(),
		w.GetRunID(),
		false,
		enums.HISTORY_EVENT_FILTER_TYPE_ALL_EVENT,
	)

	for {
		if !i.HasNext() {
			break
		}

		e, err := i.Next()
		if err != nil {
			t.Error("unable to read history event")
			break
		}

		if assert(e) {
			t.Error("found unexpected event")
			break
		}
	}
}