-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
178 lines (149 loc) · 4.16 KB
/
example_test.go
File metadata and controls
178 lines (149 loc) · 4.16 KB
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
//go:build integration
package testcontainer_test
import (
"context"
"fmt"
"log"
"testing"
"time"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/workflow"
"github.com/jasoet/pkg/v2/temporal/testcontainer"
)
// Example workflow for demonstration
func SampleWorkflow(ctx workflow.Context, name string) (string, error) {
return fmt.Sprintf("Hello, %s!", name), nil
}
// ExampleSetup demonstrates the simplest way to use testcontainer
func ExampleSetup() {
ctx := context.Background()
// Setup container and client with default configuration
_, client, cleanup, err := testcontainer.Setup(
ctx,
testcontainer.ClientConfig{
Namespace: "default",
},
testcontainer.Options{},
)
if err != nil {
log.Fatalf("Setup failed: %v", err)
}
defer cleanup()
// Now you can use the client for your tests
fmt.Println("Client connected to:", client != nil)
// Output: Client connected to: true
}
// ExampleSetup_withCustomOptions shows how to customize container options
func ExampleSetup_withCustomOptions() {
ctx := context.Background()
// Custom options
opts := testcontainer.Options{
Image: "temporalio/temporal:latest",
StartupTimeout: 90 * time.Second,
// Logger can be *testing.T or any custom logger
Logger: nil, // Set to t in actual tests
}
_, client, cleanup, err := testcontainer.Setup(
ctx,
testcontainer.ClientConfig{
Namespace: "default",
},
opts,
)
if err != nil {
log.Fatalf("Setup failed: %v", err)
}
defer cleanup()
fmt.Println("Client ready:", client != nil)
// Output: Client ready: true
}
// ExampleStart demonstrates manual container management
func ExampleStart() {
ctx := context.Background()
// Start container manually
container, err := testcontainer.Start(ctx, testcontainer.Options{})
if err != nil {
log.Fatalf("Failed to start container: %v", err)
}
defer container.Terminate(ctx)
// Get connection details
hostPort := container.HostPort()
fmt.Println("Container running at:", hostPort != "")
// Output: Container running at: true
}
// TestIntegration_FullWorkflow demonstrates a complete integration test
func TestIntegration_FullWorkflow(t *testing.T) {
ctx := context.Background()
// Setup test environment
_, client, cleanup, err := testcontainer.Setup(
ctx,
testcontainer.ClientConfig{
Namespace: "default",
},
testcontainer.Options{Logger: t},
)
if err != nil {
t.Fatalf("Setup failed: %v", err)
}
defer cleanup()
// Verify we can connect
if client == nil {
t.Fatal("Client is nil")
}
// In a real test, you would:
// 1. Start a worker
// 2. Execute workflows
// 3. Verify results
t.Log("Integration test environment ready")
}
// TestIntegration_CustomConfig demonstrates using custom Temporal configuration
func TestIntegration_CustomConfig(t *testing.T) {
ctx := context.Background()
// Create custom config
config := testcontainer.ClientConfig{
Namespace: "test-namespace",
}
container, client, cleanup, err := testcontainer.Setup(
ctx,
config,
testcontainer.Options{Logger: t},
)
if err != nil {
t.Fatalf("Setup failed: %v", err)
}
defer cleanup()
// Verify container is running
if container.HostPort() == "" {
t.Fatal("Container host port is empty")
}
// Verify client is configured correctly
if client == nil {
t.Fatal("Client is nil")
}
t.Logf("Container running at: %s", container.HostPort())
}
// TestIntegration_ManualSetup demonstrates manual setup for advanced use cases
func TestIntegration_ManualSetup(t *testing.T) {
ctx := context.Background()
// Start container with custom options
container, err := testcontainer.Start(ctx, testcontainer.Options{
Image: "temporalio/temporal:latest",
StartupTimeout: 120 * time.Second,
Logger: t,
})
if err != nil {
t.Fatalf("Failed to start container: %v", err)
}
defer container.Terminate(ctx)
// Create client directly using Temporal SDK
// You can also use your own client creation logic here
temporalClient, err := client.Dial(client.Options{
HostPort: container.HostPort(),
Namespace: "default",
})
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
defer temporalClient.Close()
t.Logf("Manual setup complete. Container at: %s", container.HostPort())
}