Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions pkg/installations/installations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func buildInstallation(installationId string, kind interfaces.DeliveryMechanismK

func Test_Register(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)

svc := createService(db)
res, err := svc.Register(ctx, buildInstallation(INSTALLATION_ID, interfaces.APNS, TOKEN))
Expand All @@ -56,8 +55,7 @@ func Test_Register(t *testing.T) {
func Test_RegisterDuplicate(t *testing.T) {
var err error
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)

svc := createService(db)

Expand Down Expand Up @@ -96,8 +94,7 @@ func Test_RegisterDuplicate(t *testing.T) {

func Test_RegisterUpdate(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)

var err error
svc := createService(db)
Expand Down Expand Up @@ -140,8 +137,7 @@ func Test_RegisterUpdate(t *testing.T) {

func Test_Delete(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)
svc := createService(db)

createReq := buildInstallation(INSTALLATION_ID, interfaces.APNS, TOKEN)
Expand All @@ -164,8 +160,7 @@ func Test_Delete(t *testing.T) {

func Test_DeleteAndRegisterAgain(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)
svc := createService(db)

createReq := buildInstallation(INSTALLATION_ID, interfaces.APNS, TOKEN)
Expand All @@ -191,8 +186,7 @@ func Test_DeleteAndRegisterAgain(t *testing.T) {

func Test_Get(t *testing.T) {
ctx := context.Background()
db, _ := test.CreateTestDb()
// defer cleanup()
db := test.CreateTestDb(t)
svc := createService(db)

installationIds := []string{"install1", "install2", "install3"}
Expand All @@ -214,8 +208,7 @@ func Test_Get(t *testing.T) {

func Test_GetMultiple(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)
svc := createService(db)

tokens := []string{"token1", "token2", "token3"}
Expand All @@ -232,8 +225,7 @@ func Test_GetMultiple(t *testing.T) {

func Test_GetDeleted(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)
svc := createService(db)

_, err := svc.Register(ctx, buildInstallation(INSTALLATION_ID, interfaces.APNS, TOKEN))
Expand Down
24 changes: 8 additions & 16 deletions pkg/subscriptions/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func createService(db *bun.DB) interfaces.Subscriptions {

func Test_Subscribe(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)

svc := createService(db)

Expand All @@ -47,8 +46,7 @@ func Test_Subscribe(t *testing.T) {

func Test_SubscribeMultiple(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)

svc := createService(db)

Expand Down Expand Up @@ -80,8 +78,7 @@ func Test_SubscribeMultiple(t *testing.T) {

func Test_Unsubscribe(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)

svc := createService(db)

Expand All @@ -103,8 +100,7 @@ func Test_Unsubscribe(t *testing.T) {

func Test_UnsubscribeResubscribe(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)

svc := createService(db)

Expand Down Expand Up @@ -135,8 +131,7 @@ func Test_UnsubscribeResubscribe(t *testing.T) {

func Test_SubscribeWithMetadata(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)

svc := createService(db)

Expand Down Expand Up @@ -165,8 +160,7 @@ func Test_SubscribeWithMetadata(t *testing.T) {

func Test_UpdateIsSilent(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)

svc := createService(db)

Expand Down Expand Up @@ -199,8 +193,7 @@ func Test_UpdateIsSilent(t *testing.T) {

func Test_UpdateHmacKeys(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)

svc := createService(db)

Expand Down Expand Up @@ -236,8 +229,7 @@ func Test_UpdateHmacKeys(t *testing.T) {

func Test_GetSubscriptions(t *testing.T) {
ctx := context.Background()
db, cleanup := test.CreateTestDb()
defer cleanup()
db := test.CreateTestDb(t)

svc := createService(db)

Expand Down
3 changes: 1 addition & 2 deletions pkg/xmtp/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func buildTestListener(t *testing.T, deliveryService interfaces.Delivery) (*List
logger := logging.CreateLogger("console", "info")
ctx, cancel := context.WithCancel(context.Background())
opts := options.XmtpOptions{ListenerEnabled: true, GrpcAddress: XMTP_ADDRESS, UseTls: false, NumWorkers: 5}
db, cleanup := test.CreateTestDb()
db := test.CreateTestDb(t)
installations := installations.NewInstallationsService(logger, db)
subscriptions := subscriptions.NewSubscriptionsService(logger, db)

Expand All @@ -43,7 +43,6 @@ func buildTestListener(t *testing.T, deliveryService interfaces.Delivery) (*List
return l, func() {
cancel()
l.Stop()
cleanup()
}
}

Expand Down
24 changes: 15 additions & 9 deletions test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"context"
"testing"
"time"

"github.com/uptrace/bun"
Expand All @@ -17,15 +18,20 @@ func createDb() *bun.DB {
return db
}

func CreateTestDb() (*bun.DB, func()) {
ctx := context.Background()
func CreateTestDb(t *testing.T) *bun.DB {
ctx := t.Context()
db := createDb()
_ = database.Migrate(ctx, db)

return db, func() {
_, _ = db.NewTruncateTable().Model((*database.Installation)(nil)).Cascade().Exec(ctx)
_, _ = db.NewTruncateTable().Model((*database.DeviceDeliveryMechanism)(nil)).Cascade().Exec(ctx)
_, _ = db.NewTruncateTable().Model((*database.Subscription)(nil)).Cascade().Exec(ctx)
_, _ = db.NewTruncateTable().Model((*database.SubscriptionHmacKeys)(nil)).Cascade().Exec(ctx)
if err := database.Migrate(ctx, db); err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
cleanupCtx := context.Background()
Copy link
Copy Markdown
Collaborator

@neekolas neekolas Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you double check that t.Context() isn't available in cleanup scripts? Point me to the relevant docs.

_, _ = db.NewTruncateTable().Model((*database.Installation)(nil)).Cascade().Exec(cleanupCtx)
_, _ = db.NewTruncateTable().Model((*database.DeviceDeliveryMechanism)(nil)).Cascade().Exec(cleanupCtx)
_, _ = db.NewTruncateTable().Model((*database.Subscription)(nil)).Cascade().Exec(cleanupCtx)
_, _ = db.NewTruncateTable().Model((*database.SubscriptionHmacKeys)(nil)).Cascade().Exec(cleanupCtx)
})

return db
}
Loading