Skip to content

Commit 45b778c

Browse files
authored
Add failing test for timer cleanup
1 parent 2995cea commit 45b778c

File tree

12 files changed

+164
-36
lines changed

12 files changed

+164
-36
lines changed

backend/mysql/mysql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
//go:embed schema.sql
2323
var schema string
2424

25-
func NewMysqlBackend(host string, port int, user, password, database string, opts ...backend.BackendOption) backend.Backend {
25+
func NewMysqlBackend(host string, port int, user, password, database string, opts ...backend.BackendOption) *mysqlBackend {
2626
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true&interpolateParams=true", user, password, host, port, database)
2727

2828
schemaDsn := dsn + "&multiStatements=true"

backend/mysql/mysql_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package mysql
22

33
import (
4+
"context"
45
"database/sql"
56
"fmt"
67
"strings"
78
"testing"
89

910
"github.com/cschleiden/go-workflows/backend"
1011
"github.com/cschleiden/go-workflows/backend/test"
12+
"github.com/cschleiden/go-workflows/internal/history"
1113
"github.com/google/uuid"
1214
)
1315

@@ -24,7 +26,7 @@ func Test_MysqlBackend(t *testing.T) {
2426

2527
var dbName string
2628

27-
test.BackendTest(t, func() backend.Backend {
29+
test.BackendTest(t, func() test.TestBackend {
2830
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/?parseTime=true&interpolateParams=true", testUser, testPassword))
2931
if err != nil {
3032
panic(err)
@@ -40,7 +42,7 @@ func Test_MysqlBackend(t *testing.T) {
4042
}
4143

4244
return NewMysqlBackend("localhost", 3306, testUser, testPassword, dbName, backend.WithStickyTimeout(0))
43-
}, func(b backend.Backend) {
45+
}, func(b test.TestBackend) {
4446
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/?parseTime=true&interpolateParams=true", testUser, testPassword))
4547
if err != nil {
4648
panic(err)
@@ -63,7 +65,7 @@ func TestMySqlBackendE2E(t *testing.T) {
6365

6466
var dbName string
6567

66-
test.EndToEndBackendTest(t, func() backend.Backend {
68+
test.EndToEndBackendTest(t, func() test.TestBackend {
6769
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/?parseTime=true&interpolateParams=true", testUser, testPassword))
6870
if err != nil {
6971
panic(err)
@@ -79,7 +81,7 @@ func TestMySqlBackendE2E(t *testing.T) {
7981
}
8082

8183
return NewMysqlBackend("localhost", 3306, testUser, testPassword, dbName, backend.WithStickyTimeout(0))
82-
}, func(b backend.Backend) {
84+
}, func(b test.TestBackend) {
8385
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/?parseTime=true&interpolateParams=true", testUser, testPassword))
8486
if err != nil {
8587
panic(err)
@@ -94,3 +96,10 @@ func TestMySqlBackendE2E(t *testing.T) {
9496
}
9597
})
9698
}
99+
100+
var _ test.TestBackend = (*mysqlBackend)(nil)
101+
102+
func (mb *mysqlBackend) GetFutureEvents(ctx context.Context) ([]history.Event, error) {
103+
// TODO: TESTING: Implement
104+
return nil, nil
105+
}

backend/redis/queue.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ func newTaskQueue[T any](rdb redis.UniversalClient, tasktype string) (*taskQueue
6363
}
6464

6565
for name, cmd := range cmds {
66-
// DEBUG: REMOVE
67-
fmt.Println(name, cmd.Val())
66+
// fmt.Println(name, cmd.Val())
6867

6968
if cmd.Err() != nil {
7069
return nil, fmt.Errorf("loading redis script: %v %w", name, cmd.Err())

backend/redis/redis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func NewRedisBackend(client redis.UniversalClient, opts ...RedisBackendOption) (
7777
"requeueInstanceCmd": requeueInstanceCmd.Load(ctx, rb.rdb),
7878
}
7979
for name, cmd := range cmds {
80-
fmt.Println(name, cmd.Val())
80+
// fmt.Println(name, cmd.Val())
8181

8282
if cmd.Err() != nil {
8383
return nil, fmt.Errorf("loading redis script: %v %w", name, cmd.Err())

backend/redis/redis_test.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package redis
22

33
import (
44
"context"
5+
"encoding/json"
6+
"fmt"
57
"strings"
68
"testing"
79
"time"
810

911
"github.com/cschleiden/go-workflows/backend"
1012
"github.com/cschleiden/go-workflows/backend/test"
13+
"github.com/cschleiden/go-workflows/internal/history"
1114
"github.com/cschleiden/go-workflows/log"
1215
"github.com/go-redis/redis/v8"
1316
)
@@ -26,7 +29,7 @@ func Benchmark_RedisBackend(b *testing.B) {
2629
client := getClient()
2730
setup := getCreateBackend(client, true)
2831

29-
test.SimpleWorkflowBenchmark(b, setup, func(b backend.Backend) {
32+
test.SimpleWorkflowBenchmark(b, setup, func(b test.TestBackend) {
3033
if err := b.(*redisBackend).Close(); err != nil {
3134
panic(err)
3235
}
@@ -66,8 +69,8 @@ func getClient() redis.UniversalClient {
6669
return client
6770
}
6871

69-
func getCreateBackend(client redis.UniversalClient, ignoreLog bool) func() backend.Backend {
70-
return func() backend.Backend {
72+
func getCreateBackend(client redis.UniversalClient, ignoreLog bool) func() test.TestBackend {
73+
return func() test.TestBackend {
7174
// Flush database
7275
if err := client.FlushDB(context.Background()).Err(); err != nil {
7376
panic(err)
@@ -125,3 +128,35 @@ func (nl *nullLogger) With(fields ...interface{}) log.Logger {
125128
}
126129

127130
var _ log.Logger = (*nullLogger)(nil)
131+
132+
var _ test.TestBackend = (*redisBackend)(nil)
133+
134+
// GetFutureEvents
135+
func (rb *redisBackend) GetFutureEvents(ctx context.Context) ([]history.Event, error) {
136+
r, err := rb.rdb.ZRangeByScore(ctx, futureEventsKey(), &redis.ZRangeBy{
137+
Min: "-inf",
138+
Max: "+inf",
139+
}).Result()
140+
141+
if err != nil {
142+
return nil, fmt.Errorf("getting future events: %w", err)
143+
}
144+
145+
events := make([]history.Event, 0)
146+
147+
for _, eventID := range r {
148+
eventStr, err := rb.rdb.HGet(ctx, eventID, "event").Result()
149+
if err != nil {
150+
return nil, fmt.Errorf("getting event %v: %w", eventID, err)
151+
}
152+
153+
var event history.Event
154+
if err := json.Unmarshal([]byte(eventStr), &event); err != nil {
155+
return nil, fmt.Errorf("unmarshaling event %v: %w", eventID, err)
156+
}
157+
158+
events = append(events, event)
159+
}
160+
161+
return events, nil
162+
}

backend/sqlite/sqlite_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
package sqlite
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/cschleiden/go-workflows/backend"
78
"github.com/cschleiden/go-workflows/backend/test"
9+
"github.com/cschleiden/go-workflows/internal/history"
810
)
911

1012
func Test_SqliteBackend(t *testing.T) {
11-
test.BackendTest(t, func() backend.Backend {
13+
test.BackendTest(t, func() test.TestBackend {
1214
// Disable sticky workflow behavior for the test execution
1315
return NewInMemoryBackend(backend.WithStickyTimeout(0))
1416
}, nil)
1517
}
1618

1719
func Test_EndToEndSqliteBackend(t *testing.T) {
18-
test.EndToEndBackendTest(t, func() backend.Backend {
20+
test.EndToEndBackendTest(t, func() test.TestBackend {
1921
// Disable sticky workflow behavior for the test execution
2022
return NewInMemoryBackend(backend.WithStickyTimeout(0))
2123
}, nil)
2224
}
25+
26+
var _ test.TestBackend = (*sqliteBackend)(nil)
27+
28+
func (sb *sqliteBackend) GetFutureEvents(ctx context.Context) ([]history.Event, error) {
29+
// TODO: TESTING: Implement
30+
return nil, nil
31+
}

backend/test/backend.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test
2+
3+
import (
4+
"context"
5+
6+
"github.com/cschleiden/go-workflows/backend"
7+
"github.com/cschleiden/go-workflows/internal/history"
8+
)
9+
10+
type TestBackend interface {
11+
backend.Backend
12+
13+
GetFutureEvents(ctx context.Context) ([]history.Event, error)
14+
}

backend/test/backendtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/stretchr/testify/require"
1616
)
1717

18-
func BackendTest(t *testing.T, setup func() backend.Backend, teardown func(b backend.Backend)) {
18+
func BackendTest(t *testing.T, setup func() TestBackend, teardown func(b TestBackend)) {
1919
tests := []struct {
2020
name string
2121
f func(t *testing.T, ctx context.Context, b backend.Backend)

backend/test/bench.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import (
66
"testing"
77
"time"
88

9-
"github.com/cschleiden/go-workflows/backend"
109
"github.com/cschleiden/go-workflows/client"
1110
"github.com/cschleiden/go-workflows/worker"
1211
"github.com/cschleiden/go-workflows/workflow"
1312
"github.com/google/uuid"
1413
"github.com/stretchr/testify/require"
1514
)
1615

17-
func SimpleWorkflowBenchmark(b *testing.B, setup func() backend.Backend, teardown func(b backend.Backend)) {
16+
func SimpleWorkflowBenchmark(b *testing.B, setup func() TestBackend, teardown func(b TestBackend)) {
1817
// Suppress default metric
1918
b.ReportMetric(0, "ns/op")
2019
b.StopTimer()

0 commit comments

Comments
 (0)