From 3849d1e2b3623b31beb50f9d007e85cf8ba1ec02 Mon Sep 17 00:00:00 2001 From: Laszlo Fogas Date: Fri, 21 May 2021 12:50:19 +0200 Subject: [PATCH] Testcases for https://github.com/laszlocph/woodpecker/issues/200 --- cncd/queue/fifo_test.go | 125 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/cncd/queue/fifo_test.go b/cncd/queue/fifo_test.go index 287107c6f..40dad4b4f 100644 --- a/cncd/queue/fifo_test.go +++ b/cncd/queue/fifo_test.go @@ -200,6 +200,131 @@ func TestFifoErrors(t *testing.T) { } } +func TestFifoErrors2(t *testing.T) { + task1 := &Task{ + ID: "1", + } + + task2 := &Task{ + ID: "2", + Dependencies: []string{"1"}, + DepStatus: make(map[string]string), + } + + task3 := &Task{ + ID: "3", + Dependencies: []string{"1", "2"}, + DepStatus: make(map[string]string), + } + + q := New().(*fifo) + q.PushAtOnce(noContext, []*Task{task2, task3, task1}) + + got, _ := q.Poll(noContext, func(*Task) bool { return true }) + if got != task1 { + t.Errorf("expect task1 returned from queue as task2 and task3 depends on it") + return + } + + q.Done(noContext, got.ID, StatusSuccess) + + got, _ = q.Poll(noContext, func(*Task) bool { return true }) + if got != task2 { + t.Errorf("expect task2 returned from queue") + return + } + + q.Error(noContext, got.ID, fmt.Errorf("exitcode 1, there was an error")) + + got, _ = q.Poll(noContext, func(*Task) bool { return true }) + if got != task3 { + t.Errorf("expect task3 returned from queue") + return + } + + if got.ShouldRun() { + t.Errorf("expect task3 should not run, task1 succeeded but task2 failed") + return + } +} + +func TestFifoErrorsMultiThread(t *testing.T) { + task1 := &Task{ + ID: "1", + } + + task2 := &Task{ + ID: "2", + Dependencies: []string{"1"}, + DepStatus: make(map[string]string), + } + + task3 := &Task{ + ID: "3", + Dependencies: []string{"1", "2"}, + DepStatus: make(map[string]string), + } + + q := New().(*fifo) + q.PushAtOnce(noContext, []*Task{task2, task3, task1}) + + obtainedWorkCh := make(chan *Task) + + for i := 0; i < 10; i++ { + go func(i int) { + for { + fmt.Printf("Worker %d started\n", i) + got, _ := q.Poll(noContext, func(*Task) bool { return true }) + obtainedWorkCh <- got + } + }(i) + } + + task1Processed := false + task2Processed := false + + for { + select { + case got := <-obtainedWorkCh: + fmt.Println(got.ID) + + if !task1Processed { + if got != task1 { + t.Errorf("expect task1 returned from queue as task2 and task3 depends on it") + return + } else { + task1Processed = true + q.Done(noContext, got.ID, StatusSuccess) + } + } else if !task2Processed { + if got != task2 { + t.Errorf("expect task2 returned from queue") + return + } else { + task2Processed = true + q.Error(noContext, got.ID, fmt.Errorf("exitcode 1, there was an error")) + } + } else { + if got != task3 { + t.Errorf("expect task3 returned from queue") + return + } + + if got.ShouldRun() { + t.Errorf("expect task3 should not run, task1 succeeded but task2 failed") + return + } else { + return + } + } + + case <-time.After(3 * time.Second): + t.Errorf("test timed out") + return + } + } +} + func TestFifoTransitiveErrors(t *testing.T) { task1 := &Task{ ID: "1",