Skip to content
New issue

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

[Unit Tests] - runCron #2244

Open
tomsmith8 opened this issue Dec 19, 2024 · 1 comment
Open

[Unit Tests] - runCron #2244

tomsmith8 opened this issue Dec 19, 2024 · 1 comment
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for "runCron"


Stakwork Run


Unit Test Code


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)
}
@Shoaibdev7
Copy link
Contributor

Shoaibdev7 commented Dec 19, 2024

@tomsmith8 assign me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants