Skip to content

Commit fb97d3d

Browse files
committed
update tests
1 parent 4a3fcd2 commit fb97d3d

File tree

8 files changed

+192
-70
lines changed

8 files changed

+192
-70
lines changed

dbos/admin_server_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ import (
1111
)
1212

1313
func TestAdminServer(t *testing.T) {
14-
databaseURL := getDatabaseURL(t)
14+
databaseURL := getDatabaseURL()
1515

1616
t.Run("Admin server is not started by default", func(t *testing.T) {
1717

18-
executor, err := NewDBOSContext(Config{
18+
ctx, err := NewDBOSContext(Config{
1919
DatabaseURL: databaseURL,
2020
AppName: "test-app",
2121
})
2222
if err != nil {
2323
t.Skipf("Failed to initialize DBOS: %v", err)
2424
}
25-
err = executor.Launch()
25+
err = ctx.Launch()
2626
if err != nil {
2727
t.Skipf("Failed to initialize DBOS: %v", err)
2828
}
2929

3030
// Ensure cleanup
3131
defer func() {
32-
if executor != nil {
33-
executor.Shutdown()
32+
if ctx != nil {
33+
ctx.Shutdown()
3434
}
3535
}()
3636

@@ -45,11 +45,11 @@ func TestAdminServer(t *testing.T) {
4545
}
4646

4747
// Verify the DBOS executor doesn't have an admin server instance
48-
if executor == nil {
48+
if ctx == nil {
4949
t.Fatal("Expected DBOS instance to be created")
5050
}
5151

52-
exec := executor.(*dbosContext)
52+
exec := ctx.(*dbosContext)
5353
if exec.adminServer != nil {
5454
t.Error("Expected admin server to be nil when not configured")
5555
}
@@ -60,35 +60,35 @@ func TestAdminServer(t *testing.T) {
6060
// (This will be handled by the individual executor cleanup)
6161

6262
// Launch DBOS with admin server once for all endpoint tests
63-
executor, err := NewDBOSContext(Config{
63+
ctx, err := NewDBOSContext(Config{
6464
DatabaseURL: databaseURL,
6565
AppName: "test-app",
6666
AdminServer: true,
6767
})
6868
if err != nil {
6969
t.Skipf("Failed to initialize DBOS with admin server: %v", err)
7070
}
71-
err = executor.Launch()
71+
err = ctx.Launch()
7272
if err != nil {
7373
t.Skipf("Failed to initialize DBOS with admin server: %v", err)
7474
}
7575

7676
// Ensure cleanup
7777
defer func() {
78-
if executor != nil {
79-
executor.Shutdown()
78+
if ctx != nil {
79+
ctx.Shutdown()
8080
}
8181
}()
8282

8383
// Give the server a moment to start
8484
time.Sleep(100 * time.Millisecond)
8585

8686
// Verify the DBOS executor has an admin server instance
87-
if executor == nil {
87+
if ctx == nil {
8888
t.Fatal("Expected DBOS instance to be created")
8989
}
9090

91-
exec := executor.(*dbosContext)
91+
exec := ctx.(*dbosContext)
9292
if exec.adminServer == nil {
9393
t.Fatal("Expected admin server to be created in DBOS instance")
9494
}

dbos/dbos_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
func TestConfigValidationErrorTypes(t *testing.T) {
8-
databaseURL := getDatabaseURL(t)
8+
databaseURL := getDatabaseURL()
99

1010
t.Run("FailsWithoutAppName", func(t *testing.T) {
1111
config := Config{

dbos/initialize_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package dbos
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// TestInitializeReturnsExecutor verifies that our updated Initialize function works correctly
8+
func TestInitializeReturnsExecutor(t *testing.T) {
9+
databaseURL := getDatabaseURL()
10+
11+
// Test that Initialize returns a DBOSExecutor
12+
ctx, err := NewDBOSContext(Config{
13+
DatabaseURL: databaseURL,
14+
AppName: "test-initialize",
15+
})
16+
if err != nil {
17+
t.Fatalf("Failed to initialize DBOS: %v", err)
18+
}
19+
defer func() {
20+
if ctx != nil {
21+
ctx.Shutdown()
22+
}
23+
}() // Clean up executor
24+
25+
if ctx == nil {
26+
t.Fatal("Initialize returned nil executor")
27+
}
28+
29+
// Test that executor implements DBOSContext interface
30+
var _ DBOSContext = ctx
31+
32+
// Test that we can call methods on the executor
33+
appVersion := ctx.GetApplicationVersion()
34+
if appVersion == "" {
35+
t.Fatal("GetApplicationVersion returned empty string")
36+
}
37+
38+
scheduler := ctx.(*dbosContext).getWorkflowScheduler()
39+
if scheduler == nil {
40+
t.Fatal("getWorkflowScheduler returned nil")
41+
}
42+
}
43+
44+
// TestWithWorkflowWithExecutor verifies that WithWorkflow works with an executor
45+
func TestWithWorkflowWithExecutor(t *testing.T) {
46+
ctx := setupDBOS(t)
47+
48+
// Test workflow function
49+
testWorkflow := func(ctx DBOSContext, input string) (string, error) {
50+
return "hello " + input, nil
51+
}
52+
53+
// Test that RegisterWorkflow works with executor
54+
RegisterWorkflow(ctx, testWorkflow)
55+
56+
// Test executing the workflow
57+
handle, err := RunAsWorkflow(ctx, testWorkflow, "world")
58+
if err != nil {
59+
t.Fatalf("Failed to execute workflow: %v", err)
60+
}
61+
62+
result, err := handle.GetResult()
63+
if err != nil {
64+
t.Fatalf("Failed to get workflow result: %v", err)
65+
}
66+
67+
expected := "hello world"
68+
if result != expected {
69+
t.Fatalf("Expected %q, got %q", expected, result)
70+
}
71+
}
72+
73+
// TestSetupDBOSReturnsExecutor verifies that setupDBOS returns an executor
74+
func TestSetupDBOSReturnsExecutor(t *testing.T) {
75+
executor := setupDBOS(t)
76+
77+
if executor == nil {
78+
t.Fatal("setupDBOS returned nil executor")
79+
}
80+
81+
// Test succeeded - executor is valid
82+
}

dbos/logger_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestLogger(t *testing.T) {
11-
databaseURL := getDatabaseURL(t)
11+
databaseURL := getDatabaseURL()
1212

1313
t.Run("Default logger", func(t *testing.T) {
1414
dbosCtx, err := NewDBOSContext(Config{

dbos/queues_test.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,14 @@ func TestWorkflowQueues(t *testing.T) {
177177
}
178178
})
179179

180+
/* TODO: we will move queue registry in the new interface in a subsequent PR
180181
t.Run("DynamicRegistration", func(t *testing.T) {
181182
q := NewWorkflowQueue("dynamic-queue")
182183
if len(q.name) > 0 {
183184
t.Fatalf("expected nil queue for dynamic registration after DBOS initialization, got %v", q)
184185
}
185186
})
187+
*/
186188

187189
t.Run("QueueWorkflowDLQ", func(t *testing.T) {
188190
workflowID := "blocking-workflow-test"
@@ -303,6 +305,11 @@ func TestQueueRecovery(t *testing.T) {
303305
}
304306
RegisterWorkflow(dbosCtx, recoveryWorkflowFunc)
305307

308+
err := dbosCtx.Launch()
309+
if err != nil {
310+
t.Fatalf("failed to launch DBOS instance: %v", err)
311+
}
312+
306313
queuedSteps := 5
307314

308315
for i := range recoveryStepEvents {
@@ -402,7 +409,7 @@ var (
402409
)
403410

404411
func TestGlobalConcurrency(t *testing.T) {
405-
dbosContext := setupDBOS(t)
412+
dbosCtx := setupDBOS(t)
406413

407414
// Create workflow with dbosContext
408415
globalConcurrencyWorkflowFunc := func(ctx DBOSContext, input string) (string, error) {
@@ -415,15 +422,20 @@ func TestGlobalConcurrency(t *testing.T) {
415422
}
416423
return input, nil
417424
}
418-
RegisterWorkflow(dbosContext, globalConcurrencyWorkflowFunc)
425+
RegisterWorkflow(dbosCtx, globalConcurrencyWorkflowFunc)
426+
427+
err := dbosCtx.Launch()
428+
if err != nil {
429+
t.Fatalf("failed to launch DBOS instance: %v", err)
430+
}
419431

420432
// Enqueue two workflows
421-
handle1, err := RunAsWorkflow(dbosContext, globalConcurrencyWorkflowFunc, "workflow1", WithQueue(globalConcurrencyQueue.name))
433+
handle1, err := RunAsWorkflow(dbosCtx, globalConcurrencyWorkflowFunc, "workflow1", WithQueue(globalConcurrencyQueue.name))
422434
if err != nil {
423435
t.Fatalf("failed to enqueue workflow1: %v", err)
424436
}
425437

426-
handle2, err := RunAsWorkflow(dbosContext, globalConcurrencyWorkflowFunc, "workflow2", WithQueue(globalConcurrencyQueue.name))
438+
handle2, err := RunAsWorkflow(dbosCtx, globalConcurrencyWorkflowFunc, "workflow2", WithQueue(globalConcurrencyQueue.name))
427439
if err != nil {
428440
t.Fatalf("failed to enqueue workflow2: %v", err)
429441
}
@@ -465,7 +477,7 @@ func TestGlobalConcurrency(t *testing.T) {
465477
if result2 != "workflow2" {
466478
t.Fatalf("expected result from workflow2 to be 'workflow2', got %v", result2)
467479
}
468-
if !queueEntriesAreCleanedUp(dbosContext) {
480+
if !queueEntriesAreCleanedUp(dbosCtx) {
469481
t.Fatal("expected queue entries to be cleaned up after global concurrency test")
470482
}
471483
}
@@ -498,6 +510,11 @@ func TestWorkerConcurrency(t *testing.T) {
498510
}
499511
RegisterWorkflow(dbosCtx, blockingWfFunc)
500512

513+
err := dbosCtx.Launch()
514+
if err != nil {
515+
t.Fatalf("failed to launch DBOS instance: %v", err)
516+
}
517+
501518
// First enqueue four blocking workflows
502519
handle1, err := RunAsWorkflow(dbosCtx, blockingWfFunc, 0, WithQueue(workerConcurrencyQueue.name), WithWorkflowID("worker-cc-wf-1"))
503520
if err != nil {
@@ -653,6 +670,11 @@ func TestWorkerConcurrencyXRecovery(t *testing.T) {
653670
}
654671
RegisterWorkflow(dbosCtx, workerConcurrencyRecoveryBlockingWf2)
655672

673+
err := dbosCtx.Launch()
674+
if err != nil {
675+
t.Fatalf("failed to launch DBOS instance: %v", err)
676+
}
677+
656678
// Enqueue two workflows on a queue with worker concurrency = 1
657679
handle1, err := RunAsWorkflow(dbosCtx, workerConcurrencyRecoveryBlockingWf1, "workflow1", WithQueue(workerConcurrencyRecoveryQueue.name), WithWorkflowID("worker-cc-x-recovery-wf-1"))
658680
if err != nil {
@@ -760,6 +782,11 @@ func TestQueueRateLimiter(t *testing.T) {
760782
// Create workflow with dbosContext
761783
RegisterWorkflow(dbosCtx, rateLimiterTestWorkflow)
762784

785+
err := dbosCtx.Launch()
786+
if err != nil {
787+
t.Fatalf("failed to launch DBOS instance: %v", err)
788+
}
789+
763790
limit := 5
764791
period := 1.8
765792
numWaves := 3

dbos/serialization_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ import (
2020
// Builtin types
2121
func encodingStepBuiltinTypes(_ context.Context, input ...any) (int, error) {
2222
if len(input) == 0 {
23+
fmt.Println("No input provided to encodingStepBuiltinTypes")
2324
return 0, errors.New("step error")
2425
}
2526
val, ok := input[0].(int)
27+
fmt.Println("Input to encodingStepBuiltinTypes:", val, "ok:", ok)
2628
if !ok {
2729
return 0, errors.New("step error")
2830
}
31+
fmt.Println("Processing input in encodingStepBuiltinTypes:", val)
2932
return val, errors.New("step error")
3033
}
3134

dbos/utils_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/jackc/pgx/v5"
1313
)
1414

15-
func getDatabaseURL(t *testing.T) string {
15+
func getDatabaseURL() string {
1616
databaseURL := os.Getenv("DBOS_SYSTEM_DATABASE_URL")
1717
if databaseURL == "" {
1818
password := os.Getenv("PGPASSWORD")
@@ -28,7 +28,7 @@ func getDatabaseURL(t *testing.T) string {
2828
func setupDBOS(t *testing.T) DBOSContext {
2929
t.Helper()
3030

31-
databaseURL := getDatabaseURL(t)
31+
databaseURL := getDatabaseURL()
3232

3333
// Clean up the test database
3434
parsedURL, err := pgx.ParseConfig(databaseURL)
@@ -113,9 +113,9 @@ func (e *Event) Clear() {
113113
/* Helpers */
114114

115115
// stopQueueRunner stops the queue runner for testing purposes
116-
func stopQueueRunner(executor DBOSContext) {
117-
if executor != nil {
118-
exec := executor.(*dbosContext)
116+
func stopQueueRunner(ctx DBOSContext) {
117+
if ctx != nil {
118+
exec := ctx.(*dbosContext)
119119
if exec.queueRunnerCancelFunc != nil {
120120
exec.queueRunnerCancelFunc()
121121
// Wait for queue runner to finish
@@ -125,9 +125,9 @@ func stopQueueRunner(executor DBOSContext) {
125125
}
126126

127127
// restartQueueRunner restarts the queue runner for testing purposes
128-
func restartQueueRunner(executor DBOSContext) {
129-
if executor != nil {
130-
exec := executor.(*dbosContext)
128+
func restartQueueRunner(ctx DBOSContext) {
129+
if ctx != nil {
130+
exec := ctx.(*dbosContext)
131131
// Create new context and cancel function
132132
// FIXME: cancellation now has to go through the DBOSContext
133133
ctx, cancel := context.WithCancel(context.Background())
@@ -155,12 +155,12 @@ func equal(a, b []int) bool {
155155
return true
156156
}
157157

158-
func queueEntriesAreCleanedUp(executor DBOSContext) bool {
158+
func queueEntriesAreCleanedUp(ctx DBOSContext) bool {
159159
maxTries := 10
160160
success := false
161161
for range maxTries {
162162
// Begin transaction
163-
exec := executor.(*dbosContext)
163+
exec := ctx.(*dbosContext)
164164
tx, err := exec.systemDB.(*systemDatabase).pool.Begin(context.Background())
165165
if err != nil {
166166
return false

0 commit comments

Comments
 (0)