Skip to content
Snippets Groups Projects
Select Git revision
  • bc11847d9fd307cd0f967300a0e2fabcc46eb0cb
  • master default protected
  • 1.31
  • 4.28.0
  • 4.27.0
  • 4.26.0
  • 4.25.5
  • 4.25.4
  • 4.25.3
  • 4.25.2
  • 4.25.1
  • 4.25.0
  • 4.24.3
  • 4.24.2
  • 4.24.1
  • 4.24.0
  • 4.23.6
  • 4.23.5
  • 4.23.4
  • 4.23.3
  • 4.23.2
  • 4.23.1
  • 4.23.0
23 results

release.nix

Blame
  • persistence_test.go 3.15 KiB
    package jobqueue
    
    import (
    	"github.com/robfig/cron/v3"
    	"github.com/stretchr/testify/assert"
    	"io/ioutil"
    	"os"
    	"testing"
    	"time"
    )
    
    func TestCreateJobAndSchedulerFromInput(t *testing.T) {
    	tests := []struct {
    		name      string
    		input     JobPersistence
    		wantJob   GenericJob
    		wantSched Scheduler
    		wantErr   bool
    	}{
    		{
    			name: "Shell Runnable and Interval Scheduler",
    			input: JobPersistence{
    				ID:         "1",
    				Priority:   1,
    				Timeout:    10 * time.Second,
    				MaxRetries: 3,
    				RetryDelay: 2 * time.Second,
    				Runnable: RunnableImport{
    					Type: "Shell",
    					Data: map[string]any{"ScriptPath": "script.sh"},
    				},
    				Scheduler: SchedulerPersistence{
    					Type:     "Interval",
    					Interval: 1 * time.Minute,
    				},
    			},
    			wantJob:   GenericJob(&Job[ShellResult]{ /* Initialization */ }),
    			wantSched: &IntervalScheduler{Interval: 1 * time.Minute},
    			wantErr:   false,
    		},
    		{
    			name: "Shell Runnable and Cron Scheduler",
    			input: JobPersistence{
    				ID:       "1",
    				Priority: 1,
    				Runnable: RunnableImport{
    					Type: "Shell",
    					Data: map[string]any{"ScriptPath": "script.sh"},
    				},
    				Scheduler: SchedulerPersistence{
    					Type: "Cron",
    					Spec: "* * * * * *",
    				},
    			},
    			wantJob:   GenericJob(&Job[ShellResult]{ /* Initialization */ }),
    			wantSched: &CronScheduler{ /* Initialization */ },
    			wantErr:   false,
    		},
    	}
    
    	for _, tt := range tests {
    		t.Run(tt.name, func(t *testing.T) {
    			gotJob, gotSchedule, err := CreateJobAndSchedulerFromPersistence(tt.input, nil)
    			if gotSchedule != nil {
    				if gotSchedule.GetType() == "Cron" {
    					cronInst := cron.New(cron.WithSeconds())
    					gotSchedule.(*CronScheduler).cron = cronInst
    					cronInst.Start()
    					defer cronInst.Stop()
    
    				}
    			}
    
    			if (err != nil) != tt.wantErr {
    				t.Errorf("CreateJobAndSchedulerFromPersistence() error = %v, wantErr %v", err, tt.wantErr)
    				return
    			}
    
    			assert.Equal(t, JobID(tt.input.ID), gotJob.GetID(), "Job ID mismatch")
    			assert.Equal(t, Priority(tt.input.Priority), gotJob.GetPriority(), "Job Priority mismatch")
    
    			assert.Equal(t, tt.input.Scheduler.Type, gotSchedule.GetType(), "Scheduler Type mismatch")
    
    		})
    	}
    }
    
    func TestReadJsonFile(t *testing.T) {
    	testContent := `[{"id": "1", "priority": 1}]`
    	tempFile, err := ioutil.TempFile("", "test.json")
    	if err != nil {
    		t.Fatal(err)
    	}
    	defer os.Remove(tempFile.Name())
    
    	if _, err := tempFile.Write([]byte(testContent)); err != nil {
    		t.Fatal(err)
    	}
    	tempFile.Close()
    
    	jobs, err := ReadJsonFile(tempFile.Name())
    	if err != nil {
    		t.Fatal(err)
    	}
    
    	if len(jobs) != 1 || jobs[0].ID != "1" || jobs[0].Priority != 1 {
    		t.Errorf("Expected job with ID '1' and priority 1, got %+v", jobs)
    	}
    }
    
    func TestReadYAMLFile(t *testing.T) {
    	testContent := `- id: "1"
      priority: 1
    `
    	tempFile, err := ioutil.TempFile("", "test.yaml")
    	if err != nil {
    		t.Fatal(err)
    	}
    	defer os.Remove(tempFile.Name())
    
    	if _, err := tempFile.Write([]byte(testContent)); err != nil {
    		t.Fatal(err)
    	}
    	tempFile.Close()
    
    	jobs, err := ReadYAMLFile(tempFile.Name())
    	if err != nil {
    		t.Fatal(err)
    	}
    
    	if len(jobs) != 1 || jobs[0].ID != "1" || jobs[0].Priority != 1 {
    		t.Errorf("Expected job with ID '1' and priority 1, got %+v", jobs)
    	}
    }