We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stakwork Run
File: /tmp/stakwork/sphinx-tribes/main.go
package main import ( "testing" "time" "github.com/stretchr/testify/assert" "github.com/robfig/cron/v3" "github.com/stretchr/testify/mock" ) // Mock for handlers.InitV2PaymentsCron type MockHandler struct { mock.Mock } func (m *MockHandler) InitV2PaymentsCron() { m.Called() } func TestRunCron(t *testing.T) { mockHandler := new(MockHandler) originalHandler := handlers.InitV2PaymentsCron defer func() { handlers.InitV2PaymentsCron = originalHandler }() handlers.InitV2PaymentsCron = mockHandler.InitV2PaymentsCron tests := []struct { name string cronExpression string expectedCalls int expectError bool }{ { name: "Basic Functionality: Cron Job Setup", cronExpression: "@every 0h30m0s", expectedCalls: 0, // No calls expected immediately expectError: false, }, { name: "Basic Functionality: Cron Job Execution", cronExpression: "@every 0h30m0s", expectedCalls: 1, // Expect one call after triggering expectError: false, }, { name: "Edge Case: Immediate Execution", cronExpression: "@every 0h30m0s", expectedCalls: 1, // Expect immediate call expectError: false, }, { name: "Error Condition: Invalid Cron Expression", cronExpression: "@every invalid", expectedCalls: 0, // No calls expected due to invalid expression expectError: true, }, { name: "Performance and Scale: High Frequency Execution", cronExpression: "@every 1s", expectedCalls: 1, // Expect at least one call expectError: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { c := cron.New() mockHandler.On("InitV2PaymentsCron").Return().Times(tt.expectedCalls) // Add the cron job with the specified expression _, err := c.AddFunc(tt.cronExpression, handlers.InitV2PaymentsCron) if tt.expectError { assert.Error(t, err) } else { assert.NoError(t, err) } c.Start() defer c.Stop() // Wait for a short duration to allow the cron job to trigger time.Sleep(2 * time.Second) // Assert the expected number of calls mockHandler.AssertNumberOfCalls(t, "InitV2PaymentsCron", tt.expectedCalls) }) } } func TestCronStopAndRestart(t *testing.T) { mockHandler := new(MockHandler) originalHandler := handlers.InitV2PaymentsCron defer func() { handlers.InitV2PaymentsCron = originalHandler }() handlers.InitV2PaymentsCron = mockHandler.InitV2PaymentsCron c := cron.New() mockHandler.On("InitV2PaymentsCron").Return().Times(1) _, err := c.AddFunc("@every 1s", handlers.InitV2PaymentsCron) assert.NoError(t, err) c.Start() time.Sleep(2 * time.Second) c.Stop() // Assert the expected number of calls before restart mockHandler.AssertNumberOfCalls(t, "InitV2PaymentsCron", 1) // Restart the cron c.Start() time.Sleep(2 * time.Second) c.Stop() // Assert the expected number of calls after restart mockHandler.AssertNumberOfCalls(t, "InitV2PaymentsCron", 2) } func TestMultipleCronJobs(t *testing.T) { mockHandler1 := new(MockHandler) mockHandler2 := new(MockHandler) originalHandler := handlers.InitV2PaymentsCron defer func() { handlers.InitV2PaymentsCron = originalHandler }() handlers.InitV2PaymentsCron = mockHandler1.InitV2PaymentsCron c := cron.New() mockHandler1.On("InitV2PaymentsCron").Return().Times(1) mockHandler2.On("InitV2PaymentsCron").Return().Times(1) _, err1 := c.AddFunc("@every 1s", handlers.InitV2PaymentsCron) assert.NoError(t, err1) _, err2 := c.AddFunc("@every 1s", mockHandler2.InitV2PaymentsCron) assert.NoError(t, err2) c.Start() time.Sleep(2 * time.Second) c.Stop() // Assert the expected number of calls for both handlers mockHandler1.AssertNumberOfCalls(t, "InitV2PaymentsCron", 1) mockHandler2.AssertNumberOfCalls(t, "InitV2PaymentsCron", 1) }
The text was updated successfully, but these errors were encountered:
@tomsmith8 assign me?
Sorry, something went wrong.
No branches or pull requests
Unit Test Coverage for "runCron"
Stakwork Run
Unit Test Code
File: /tmp/stakwork/sphinx-tribes/main.go
The text was updated successfully, but these errors were encountered: