-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain_test.go
67 lines (60 loc) · 1.72 KB
/
main_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
package main
import (
"context"
"io"
"log/slog"
"math"
"sync"
"testing"
"time"
mutexv1 "github.com/cludden/protoc-gen-go-temporal/gen/example/mutex/v1"
"github.com/cludden/protoc-gen-go-temporal/gen/example/mutex/v1/mutexv1xns"
"github.com/stretchr/testify/require"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/log"
"go.temporal.io/sdk/testsuite"
"go.temporal.io/sdk/worker"
"google.golang.org/protobuf/types/known/durationpb"
)
func TestSampleWorkflowWithMutexWorkflow(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx, require := context.Background(), require.New(t)
srv, err := testsuite.StartDevServer(ctx, testsuite.DevServerOptions{
ClientOptions: &client.Options{
Logger: log.NewStructuredLogger(slog.New(slog.NewJSONHandler(io.Discard, nil))),
},
})
require.NoError(err)
defer srv.Stop()
c := srv.Client()
defer c.Close()
client := mutexv1.NewExampleClient(c)
w := worker.New(c, mutexv1.ExampleTaskQueue, worker.Options{})
mutexv1.RegisterExampleWorkflows(w, &Workflows{})
mutexv1xns.RegisterExampleActivities(w, client)
require.NoError(w.Start())
defer w.Stop()
var a, b time.Time
var g sync.WaitGroup
g.Add(2)
go func() {
defer g.Done()
require.NoError(client.SampleWorkflowWithMutex(ctx, &mutexv1.SampleWorkflowWithMutexInput{
ResourceId: "test",
Sleep: durationpb.New(time.Second * 3),
}))
a = time.Now()
}()
go func() {
defer g.Done()
require.NoError(client.SampleWorkflowWithMutex(ctx, &mutexv1.SampleWorkflowWithMutexInput{
ResourceId: "test",
Sleep: durationpb.New(time.Second * 3),
}))
b = time.Now()
}()
g.Wait()
require.GreaterOrEqual(math.Abs(a.Sub(b).Seconds()), float64(3), "one workflow should finish at least 3s after the other")
}