Skip to content

Commit ec81187

Browse files
authored
Mandatory context to dboscontext (#117)
Also small tweak to CLI test to embed its test app.
1 parent 050cfef commit ec81187

File tree

9 files changed

+31
-35
lines changed

9 files changed

+31
-35
lines changed

cmd/dbos/cli_integration_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"database/sql"
7+
_ "embed"
78
"encoding/json"
89
"fmt"
910
"io"
@@ -23,6 +24,9 @@ import (
2324
"github.com/stretchr/testify/require"
2425
)
2526

27+
//go:embed cli_test_app.go.test
28+
var testAppContent []byte
29+
2630
// Test configuration
2731
const (
2832
testProjectName = "test-project"
@@ -146,12 +150,7 @@ func testProjectInitialization(t *testing.T, cliPath string) {
146150
require.NoError(t, err)
147151
assert.Contains(t, string(mainGoContent), testProjectName, "main.go should contain project name")
148152

149-
// Copy the test app to replace the template main.go
150-
testAppPath := filepath.Join(filepath.Dir(cliPath), "cli_test_app.go.test")
151-
testAppContent, err := os.ReadFile(testAppPath)
152-
require.NoError(t, err, "Failed to read test app: %s", testAppPath)
153-
154-
// Replace the template main.go with our test app
153+
// Replace the template main.go with our embedded test app
155154
mainGoPath := filepath.Join(projectDir, "main.go")
156155
err = os.WriteFile(mainGoPath, testAppContent, 0644)
157156
require.NoError(t, err, "Failed to write test app to main.go")

cmd/dbos/templates/dbos-toolbox/main.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func ScheduledWorkflow(ctx dbos.DBOSContext, scheduledTime time.Time) (string, e
9090
func main() {
9191
// Create DBOS context
9292
var err error
93-
dbosCtx, err = dbos.NewDBOSContext(dbos.Config{
93+
dbosCtx, err = dbos.NewDBOSContext(context.Background(), dbos.Config{
9494
DatabaseURL: os.Getenv("DBOS_SYSTEM_DATABASE_URL"),
9595
AppName: "{{.ProjectName}}",
9696
AdminServer: true,

cmd/dbos/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func getDBURL(_ *cobra.Command) (string, error) {
7878
func createDBOSContext(userContext context.Context, dbURL string) (dbos.DBOSContext, error) {
7979
appName := "dbos-cli"
8080

81-
ctx, err := dbos.NewDBOSContext(dbos.Config{
81+
ctx, err := dbos.NewDBOSContext(context.Background(), dbos.Config{
8282
DatabaseURL: dbURL,
8383
AppName: appName,
8484
Logger: initLogger(slog.LevelError),

dbos/admin_server_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dbos
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -19,7 +20,7 @@ func TestAdminServer(t *testing.T) {
1920
databaseURL := getDatabaseURL()
2021

2122
t.Run("Admin server is not started by default", func(t *testing.T) {
22-
ctx, err := NewDBOSContext(Config{
23+
ctx, err := NewDBOSContext(context.Background(), Config{
2324
DatabaseURL: databaseURL,
2425
AppName: "test-app",
2526
})
@@ -50,7 +51,7 @@ func TestAdminServer(t *testing.T) {
5051
t.Run("Admin server endpoints", func(t *testing.T) {
5152
resetTestDatabase(t, databaseURL)
5253
// Launch DBOS with admin server once for all endpoint tests
53-
ctx, err := NewDBOSContext(Config{
54+
ctx, err := NewDBOSContext(context.Background(), Config{
5455
DatabaseURL: databaseURL,
5556
AppName: "test-app",
5657
AdminServer: true,
@@ -214,7 +215,7 @@ func TestAdminServer(t *testing.T) {
214215

215216
t.Run("List workflows input/output values", func(t *testing.T) {
216217
resetTestDatabase(t, databaseURL)
217-
ctx, err := NewDBOSContext(Config{
218+
ctx, err := NewDBOSContext(context.Background(), Config{
218219
DatabaseURL: databaseURL,
219220
AppName: "test-app",
220221
AdminServer: true,
@@ -361,7 +362,7 @@ func TestAdminServer(t *testing.T) {
361362

362363
t.Run("List endpoints time filtering", func(t *testing.T) {
363364
resetTestDatabase(t, databaseURL)
364-
ctx, err := NewDBOSContext(Config{
365+
ctx, err := NewDBOSContext(context.Background(), Config{
365366
DatabaseURL: databaseURL,
366367
AppName: "test-app",
367368
AdminServer: true,
@@ -535,7 +536,7 @@ func TestAdminServer(t *testing.T) {
535536

536537
t.Run("ListQueuedWorkflows", func(t *testing.T) {
537538
resetTestDatabase(t, databaseURL)
538-
ctx, err := NewDBOSContext(Config{
539+
ctx, err := NewDBOSContext(context.Background(), Config{
539540
DatabaseURL: databaseURL,
540541
AppName: "test-app",
541542
AdminServer: true,
@@ -721,7 +722,7 @@ func TestAdminServer(t *testing.T) {
721722

722723
t.Run("TestDeactivate", func(t *testing.T) {
723724
resetTestDatabase(t, databaseURL)
724-
ctx, err := NewDBOSContext(Config{
725+
ctx, err := NewDBOSContext(context.Background(), Config{
725726
DatabaseURL: databaseURL,
726727
AppName: "test-app",
727728
AdminServer: true,

dbos/dbos.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,11 @@ func (c *dbosContext) GetApplicationID() string {
296296
// if err := ctx.Launch(); err != nil {
297297
// log.Fatal(err)
298298
// }
299-
func NewDBOSContext(inputConfig Config) (DBOSContext, error) {
300-
var baseCtx context.Context
301-
if inputConfig.Context != nil {
302-
baseCtx = inputConfig.Context
303-
} else {
304-
baseCtx = context.Background()
305-
}
306-
ctx, cancelFunc := context.WithCancelCause(baseCtx)
299+
func NewDBOSContext(ctx context.Context, inputConfig Config) (DBOSContext, error) {
300+
dbosBaseCtx, cancelFunc := context.WithCancelCause(ctx)
307301
initExecutor := &dbosContext{
308302
workflowsWg: &sync.WaitGroup{},
309-
ctx: ctx,
303+
ctx: dbosBaseCtx,
310304
ctxCancelFunc: cancelFunc,
311305
workflowRegistry: &sync.Map{},
312306
workflowCustomNametoFQN: &sync.Map{},

dbos/dbos_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestConfig(t *testing.T) {
1616
t.Setenv("DBOS__APPVERSION", "v1.0.0")
1717
t.Setenv("DBOS__APPID", "test-app-id")
1818
t.Setenv("DBOS__VMID", "test-executor-id")
19-
ctx, err := NewDBOSContext(Config{
19+
ctx, err := NewDBOSContext(context.Background(), Config{
2020
DatabaseURL: databaseURL,
2121
AppName: "test-initialize",
2222
})
@@ -46,7 +46,7 @@ func TestConfig(t *testing.T) {
4646
DatabaseURL: databaseURL,
4747
}
4848

49-
_, err := NewDBOSContext(config)
49+
_, err := NewDBOSContext(context.Background(), config)
5050
require.Error(t, err)
5151

5252
dbosErr, ok := err.(*DBOSError)
@@ -63,7 +63,7 @@ func TestConfig(t *testing.T) {
6363
AppName: "test-app",
6464
}
6565

66-
_, err := NewDBOSContext(config)
66+
_, err := NewDBOSContext(context.Background(), config)
6767
require.Error(t, err)
6868

6969
dbosErr, ok := err.(*DBOSError)
@@ -81,7 +81,7 @@ func TestConfig(t *testing.T) {
8181
t.Setenv("DBOS__APPVERSION", "")
8282
t.Setenv("DBOS__VMID", "")
8383

84-
ctx, err := NewDBOSContext(Config{
84+
ctx, err := NewDBOSContext(context.Background(), Config{
8585
DatabaseURL: databaseURL,
8686
AppName: "test-config-values",
8787
ApplicationVersion: "config-v1.2.3",
@@ -102,7 +102,7 @@ func TestConfig(t *testing.T) {
102102
t.Setenv("DBOS__APPVERSION", "env-v2.0.0")
103103
t.Setenv("DBOS__VMID", "env-executor-456")
104104

105-
ctx, err := NewDBOSContext(Config{
105+
ctx, err := NewDBOSContext(context.Background(), Config{
106106
DatabaseURL: databaseURL,
107107
AppName: "test-env-override",
108108
ApplicationVersion: "config-v1.2.3",
@@ -125,7 +125,7 @@ func TestConfig(t *testing.T) {
125125
t.Setenv("DBOS__APPVERSION", "")
126126
t.Setenv("DBOS__VMID", "")
127127

128-
ctx, err := NewDBOSContext(Config{
128+
ctx, err := NewDBOSContext(context.Background(), Config{
129129
DatabaseURL: databaseURL,
130130
AppName: "test-defaults",
131131
// ApplicationVersion and ExecutorID left empty
@@ -150,7 +150,7 @@ func TestConfig(t *testing.T) {
150150
t.Setenv("DBOS__APPVERSION", "env-only-v3.0.0")
151151
t.Setenv("DBOS__VMID", "env-only-executor")
152152

153-
ctx, err := NewDBOSContext(Config{
153+
ctx, err := NewDBOSContext(context.Background(), Config{
154154
DatabaseURL: databaseURL,
155155
AppName: "test-env-only",
156156
// ApplicationVersion and ExecutorID left empty
@@ -173,7 +173,7 @@ func TestConfig(t *testing.T) {
173173
t.Setenv("DBOS__APPID", "test-migration")
174174
t.Setenv("DBOS__VMID", "test-executor-id")
175175

176-
ctx, err := NewDBOSContext(Config{
176+
ctx, err := NewDBOSContext(context.Background(), Config{
177177
DatabaseURL: databaseURL,
178178
AppName: "test-migration",
179179
})
@@ -255,7 +255,7 @@ func TestConfig(t *testing.T) {
255255
ctx.Shutdown(1 * time.Minute)
256256

257257
// Recreate context - should have no error since DB is already migrated
258-
ctx2, err := NewDBOSContext(Config{
258+
ctx2, err := NewDBOSContext(context.Background(), Config{
259259
DatabaseURL: databaseURL,
260260
AppName: "test-migration-recreate",
261261
})

dbos/logger_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dbos
22

33
import (
44
"bytes"
5+
"context"
56
"log/slog"
67
"testing"
78
"time"
@@ -14,7 +15,7 @@ func TestLogger(t *testing.T) {
1415
databaseURL := getDatabaseURL()
1516

1617
t.Run("Default logger", func(t *testing.T) {
17-
dbosCtx, err := NewDBOSContext(Config{
18+
dbosCtx, err := NewDBOSContext(context.Background(), Config{
1819
DatabaseURL: databaseURL,
1920
AppName: "test-app",
2021
}) // Create executor with default logger
@@ -46,7 +47,7 @@ func TestLogger(t *testing.T) {
4647
// Add some context to the slog logger
4748
slogLogger = slogLogger.With("service", "dbos-test", "environment", "test")
4849

49-
dbosCtx, err := NewDBOSContext(Config{
50+
dbosCtx, err := NewDBOSContext(context.Background(), Config{
5051
DatabaseURL: databaseURL,
5152
AppName: "test-app",
5253
Logger: slogLogger,

dbos/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func setupDBOS(t *testing.T, dropDB bool, checkLeaks bool) DBOSContext {
5858
resetTestDatabase(t, databaseURL)
5959
}
6060

61-
dbosCtx, err := NewDBOSContext(Config{
61+
dbosCtx, err := NewDBOSContext(context.Background(), Config{
6262
DatabaseURL: databaseURL,
6363
AppName: "test-app",
6464
})

dbos/workflows_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,7 @@ func TestSendRecv(t *testing.T) {
16571657
require.Equal(t, "DBOS.sleep", receiveSteps[1].StepName, "expected step 1 to have StepName 'DBOS.sleep'")
16581658
require.Equal(t, "DBOS.recv", receiveSteps[2].StepName, "expected step 2 to have StepName 'DBOS.recv'")
16591659
require.Equal(t, "DBOS.recv", receiveSteps[3].StepName, "expected step 3 to have StepName 'DBOS.recv'")
1660+
16601661
})
16611662
t.Run("SendRecvIdempotency", func(t *testing.T) {
16621663
// Start the receive workflow and wait for it to be ready

0 commit comments

Comments
 (0)