Skip to content

Commit 6263007

Browse files
committed
unit test
1 parent 296907b commit 6263007

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

README.md

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

99
**The open-source alternative to Mailchimp, Brevo, Mailjet, Listmonk, Mailerlite, and Klaviyo, Loop.so, etc.**
1010

11-
Notifuse is a modern, self-hosted email marketing platform that allows you to send newsletters and transactional emails at a fraction of the cost. Built with Go and React, it provides enterprise-grade features with the flexibility of open-source software.
11+
Notifuse is a modern, self-hosted emailing platform that allows you to send newsletters and transactional emails at a fraction of the cost. Built with Go and React, it provides enterprise-grade features with the flexibility of open-source software.
1212

1313
<img src="https://mintlify.s3.us-west-1.amazonaws.com/notifuse/images/email_editor.png" alt="Email Editor">
1414

internal/app/app_test.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ func setupTestDBMock() (*sql.DB, sqlmock.Sqlmock, error) {
6565
mock.ExpectBegin()
6666
mock.ExpectCommit()
6767

68+
// Expect Close to be called during shutdown
69+
mock.ExpectClose()
70+
6871
return db, mock, nil
6972
}
7073

@@ -119,6 +122,7 @@ func TestAppInitMailer(t *testing.T) {
119122

120123
mockLogger := pkgmocks.NewMockLogger(ctrl)
121124
mockLogger.EXPECT().Info(gomock.Any()).AnyTimes()
125+
mockLogger.EXPECT().Warn(gomock.Any()).AnyTimes()
122126
app := NewApp(cfg, WithLogger(mockLogger))
123127
err := app.InitMailer()
124128
assert.NoError(t, err)
@@ -144,10 +148,19 @@ func TestAppShutdown(t *testing.T) {
144148
ctrl := gomock.NewController(t)
145149
defer ctrl.Finish()
146150

147-
mockDB, _, err := sqlmock.New()
151+
mockDB, mock, err := sqlmock.New()
148152
require.NoError(t, err)
149153

154+
// Expect Close to be called during shutdown
155+
mock.ExpectClose()
156+
150157
mockLogger := pkgmocks.NewMockLogger(ctrl)
158+
// Set up logger expectations for graceful shutdown messages
159+
mockLogger.EXPECT().Info(gomock.Any()).AnyTimes()
160+
mockLogger.EXPECT().WithField(gomock.Any(), gomock.Any()).Return(mockLogger).AnyTimes()
161+
mockLogger.EXPECT().WithFields(gomock.Any()).Return(mockLogger).AnyTimes()
162+
mockLogger.EXPECT().Warn(gomock.Any()).AnyTimes()
163+
mockLogger.EXPECT().Error(gomock.Any()).AnyTimes()
151164

152165
// Create app with mock DB
153166
app := NewApp(cfg, WithLogger(mockLogger), WithMockDB(mockDB))
@@ -209,15 +222,24 @@ func TestAppStart(t *testing.T) {
209222

210223
mockLogger := pkgmocks.NewMockLogger(ctrl)
211224
mockLogger.EXPECT().WithField(gomock.Any(), gomock.Any()).Return(mockLogger).AnyTimes()
225+
mockLogger.EXPECT().WithFields(gomock.Any()).Return(mockLogger).AnyTimes()
212226
mockLogger.EXPECT().Info(gomock.Any()).AnyTimes()
227+
mockLogger.EXPECT().Warn(gomock.Any()).AnyTimes()
213228
mockLogger.EXPECT().Error(gomock.Any()).AnyTimes()
214229

215-
mockDB, _, err := setupTestDBMock()
230+
// Create a simple mock DB for this test
231+
mockDB, mock, err := sqlmock.New()
216232
require.NoError(t, err)
217233
defer mockDB.Close()
218234

235+
// Only expect Close to be called during shutdown
236+
mock.ExpectClose()
237+
219238
app := NewApp(cfg, WithLogger(mockLogger), WithMockDB(mockDB))
220239

240+
// Set a shorter shutdown timeout for testing
241+
app.SetShutdownTimeout(2 * time.Second)
242+
221243
// Set up a channel to receive errors
222244
errCh := make(chan error, 1)
223245

@@ -236,8 +258,8 @@ func TestAppStart(t *testing.T) {
236258
// Verify server was created
237259
assert.True(t, app.IsServerCreated(), "Server should be created")
238260

239-
// Shutdown the server
240-
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 1*time.Second)
261+
// Shutdown the server with sufficient timeout
262+
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
241263
defer shutdownCancel()
242264

243265
err = app.Shutdown(shutdownCtx)
@@ -584,6 +606,9 @@ func TestAppStartTLS(t *testing.T) {
584606
// Create app
585607
app := NewApp(cfg)
586608

609+
// Set a shorter shutdown timeout for testing
610+
app.SetShutdownTimeout(2 * time.Second)
611+
587612
// Start server in goroutine
588613
errCh := make(chan error, 1)
589614
go func() {
@@ -596,8 +621,8 @@ func TestAppStartTLS(t *testing.T) {
596621
started := app.WaitForServerStart(ctx)
597622
require.True(t, started, "Server should have started within timeout")
598623

599-
// Shutdown the server
600-
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 1*time.Second)
624+
// Shutdown the server with sufficient timeout
625+
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
601626
defer shutdownCancel()
602627
err := app.Shutdown(shutdownCtx)
603628
assert.NoError(t, err)

0 commit comments

Comments
 (0)