github.com/swaros/contxt/module/taskrun@v0.0.0-20240305083542-3dbd4436ac40/taskwatcher_test.go (about)

     1  package taskrun
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestTimout(t *testing.T) {
     9  	ResetAllTaskInfos()
    10  	incTaskCount("testTask")
    11  	incTaskCount("testTask")
    12  
    13  	waitHits := 0
    14  	timeOutHit := false
    15  
    16  	var tasks []string
    17  	tasks = append(tasks, "testTask")
    18  	WaitForTasksDone(tasks, 30*time.Millisecond, time.Millisecond, func() bool {
    19  		waitHits++
    20  		return true
    21  	}, func() {}, func() {
    22  		// timeout
    23  		timeOutHit = true
    24  	}, func(targetFull string, target string, args map[string]string) bool {
    25  		return true
    26  	})
    27  
    28  	if waitHits == 0 {
    29  		t.Error("never waits for complete the tasks..", waitHits)
    30  	}
    31  
    32  	if timeOutHit == false {
    33  		t.Error("timeout never called..", waitHits)
    34  	}
    35  }
    36  
    37  /*
    38  func TestRegular(t *testing.T) {
    39  	ResetAllTaskInfos()
    40  	incTaskCount("testTask")
    41  	incTaskCount("testTask")
    42  	incTaskCount("testTask")
    43  	incTaskCount("testTask")
    44  
    45  	waitHits := 0
    46  	timeOutHit := false
    47  	doneCalled := false
    48  
    49  	var tasks []string
    50  	tasks = append(tasks, "testTask")
    51  	WaitForTasksDone(tasks, 30*time.Millisecond, time.Millisecond, func() bool {
    52  		waitHits++
    53  		incTaskDoneCount("testTask")
    54  		return true
    55  	}, func() {
    56  		doneCalled = true
    57  	}, func() {
    58  		// timeout
    59  		timeOutHit = true
    60  	}, func(target string) bool {
    61  		return true
    62  	})
    63  
    64  	if waitHits == 0 {
    65  		t.Error("never waits for complete the tasks..", waitHits)
    66  	}
    67  
    68  	if timeOutHit == true {
    69  		t.Error("timeout should not be called..", waitHits)
    70  	}
    71  
    72  	if doneCalled == false {
    73  		t.Error("done should be called..", waitHits)
    74  	}
    75  }
    76  */
    77  func TestNeverStarts(t *testing.T) {
    78  	ResetAllTaskInfos()
    79  
    80  	waitHits := 0
    81  	timeOutHit := false
    82  	doneCalled := false
    83  	notStartedCalled := false
    84  
    85  	var tasks []string
    86  	tasks = append(tasks, "testTask")
    87  	WaitForTasksDone(tasks, 30*time.Millisecond, time.Millisecond, func() bool {
    88  		waitHits++
    89  		//incTaskDoneCount("testTask")
    90  		return true
    91  	}, func() {
    92  		doneCalled = true
    93  	}, func() {
    94  		// timeout
    95  		timeOutHit = true
    96  	}, func(_ string, _ string, _ map[string]string) bool {
    97  		notStartedCalled = true
    98  		return false
    99  	})
   100  
   101  	if waitHits != 0 {
   102  		t.Error("there is nothing to wait for .. bit got wait hits: ", waitHits)
   103  	}
   104  
   105  	if timeOutHit == true {
   106  		t.Error("timeout should not be called..", waitHits)
   107  	}
   108  
   109  	if doneCalled == false {
   110  		t.Error("done should be called..", waitHits)
   111  	}
   112  	if notStartedCalled == false {
   113  		t.Error("not started should be triggered..", waitHits)
   114  	}
   115  }