func TestTaskManagerProviderOrder(t *testing.T) { tm := kamaji.NewTaskManager() job := kamaji.NewJob("JOB") task := kamaji.NewTask("TASK", job, []string{}) _ = kamaji.NewCommand("0", task) _ = kamaji.NewCommand("1", task) _ = kamaji.NewCommand("2", task) _ = kamaji.NewCommand("3", task) _ = kamaji.NewCommand("4", task) _ = kamaji.NewCommand("5", task) tm.AddJob(job) tm.Start() defer tm.Stop() expected := []string{"0", "1", "2", "3", "4", "5"} var got []string for _ = range task.Commands { command := <-tm.NextCommand got = append(got, command.Name) } if !testEq(expected, got) { t.Errorf("Expected %+v got %+v.", expected, got) } got = got[:0] tm.ResetProvider() for _ = range task.Commands { command := <-tm.NextCommand got = append(got, command.Name) } if !testEq(expected, got) { t.Errorf("After reset. Expected %+v got %+v.", expected, got) } }
func TestTaskManagerProviderPriority(t *testing.T) { tm := kamaji.NewTaskManager() job0 := kamaji.NewJob("JOB0") job0.SetPrio(0) job1 := kamaji.NewJob("JOB0") job1.SetPrio(1) task0 := kamaji.NewTask("TASK0", job0, []string{}) _ = kamaji.NewCommand("0", task0) tm.AddJob(job0) task1 := kamaji.NewTask("TASK1", job1, []string{}) _ = kamaji.NewCommand("1", task1) tm.AddJob(job1) tm.Start() defer tm.Stop() expected := []string{"1", "0"} var got []string for _ = range tm.Jobs { command := <-tm.NextCommand got = append(got, command.Name) } if !testEq(expected, got) { t.Errorf("Expected %+v got %+v.", expected, got) } got = got[:0] tm.ResetProvider() for _ = range tm.Jobs { command := <-tm.NextCommand got = append(got, command.Name) } if !testEq(expected, got) { t.Errorf("After reset. Expected %+v got %+v.", expected, got) } }
func TestDispatcher(t *testing.T) { fmt.Println("Starting") lm := kamaji.NewLicenseManager() nm := kamaji.NewNodeManager("", 6666) go nm.Start() tm := kamaji.NewTaskManager() d := kamaji.NewDispatcher(lm, nm, tm) job_count := 10 task_count := 5 command_count := 20 for i := 0; i < job_count; i++ { job := kamaji.NewJob(fmt.Sprintf("Job %d", i)) for j := 0; j < task_count; j++ { task := kamaji.NewTask(fmt.Sprintf("Task %d", j), job, []string{}) for k := 0; k < command_count; k++ { _ = kamaji.NewCommand(fmt.Sprintf("Command %d", k), task) } } tm.AddJob(job) } go d.Start() client_count := 1 fmt.Println("Starting: ", client_count, " clients.") quit := make(chan bool, client_count) for i := 0; i < client_count; i++ { go cli(i, quit) time.Sleep(time.Millisecond * 2) } time.Sleep(time.Second) for i := 0; i < client_count; i++ { quit <- true } }
func main() { fmt.Println("Starting") // Create all managers lm := kamaji.NewLicenseManager() lm.AddLicense("maya", 4) lm.AddLicense("nuke", 4) nm := kamaji.NewNodeManager("", 1314) tm := kamaji.NewTaskManager() d := kamaji.NewDispatcher(lm, nm, tm) s := kamaji.NewService("", 8080, lm, nm, tm) // Create signal handler c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func() { for sig := range c { switch sig { case os.Interrupt: fmt.Printf("\nCatched Interupt Signal, Cleaning up\n") d.Stop() } } }() // Create Test Jobs job_count := 10 task_count := 2 command_count := 50 for i := 1; i < job_count+1; i++ { lic_name := "maya" if i > 6 { lic_name = "nuke" } job := kamaji.NewJob(fmt.Sprintf("Job %d | %s", i, lic_name)) for j := 0; j < task_count; j++ { task := kamaji.NewTask(fmt.Sprintf("Task %d [%s]", j, lic_name), job, []string{lic_name}) for k := 0; k < command_count; k++ { _ = kamaji.NewCommand(fmt.Sprintf("Command %d", k), task) } } tm.AddJob(job) } go nm.Start() defer nm.Stop() go tm.Start() defer tm.Stop() go s.Start() defer s.Stop() d.Start() }
func TestTaskManagerGetById(t *testing.T) { tm := kamaji.NewTaskManager() job := kamaji.NewJob("JOB") task := kamaji.NewTask("TASK", job, []string{}) command := kamaji.NewCommand("1", task) tm.AddJob(job) q_job := tm.GetJobFromId(job.ID.String()) if q_job.ID.String() != job.ID.String() { t.Logf("Expected %s, got %s", job.ID, q_job.ID) } q_cmd := tm.GetCommandsFromId(command.ID.String()) if q_cmd.ID.String() != command.ID.String() { t.Logf("Expected %s, got %s", command.ID, q_cmd.ID) } }
func TestTaskManager(t *testing.T) { job_count := 1000 task_count := 2 command_count := 100 tm := kamaji.NewTaskManager() fillTaskManager(tm, job_count, task_count, command_count) expected := job_count got := tm.GetNumJobs() if got != expected { t.Errorf("Expected taskmanager to contain %d jobs but it reported %d jobs.", expected, got) } expected = task_count * job_count got = tm.GetNumTasks() if got != expected { t.Errorf("Expected taskmanager to contain %d tasks but it reported %d tasks.", expected, got) } expected = command_count * task_count * job_count got = tm.GetNumCommands() if got != expected { t.Errorf("Expected taskmanager to contain %d commands but it reported %d commands.", expected, got) } }