github.com/Axway/agent-sdk@v1.1.101/pkg/jobs/intervaljob_test.go (about)

     1  package jobs
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  type intervalJobImpl struct {
    13  	Job
    14  	name        string
    15  	runTime     time.Duration
    16  	ready       bool
    17  	executions  int
    18  	failEvery   int
    19  	status      error
    20  	failTime    time.Duration
    21  	wasFailed   bool
    22  	wasRestored bool
    23  	jobMutex    *sync.Mutex
    24  	readyMutex  *sync.Mutex
    25  	statusMutex *sync.Mutex
    26  }
    27  
    28  func (j *intervalJobImpl) Execute() error {
    29  	j.incrementExecutions()
    30  	time.Sleep(j.runTime)
    31  	if j.failEvery > 0 && j.getExecutions()%j.failEvery == 0 {
    32  		j.setWasFailed(true)
    33  		go func() {
    34  			time.Sleep(j.failTime)
    35  			j.setStatus(nil)
    36  			j.setWasRestored(true)
    37  		}()
    38  	}
    39  	return nil
    40  }
    41  
    42  func (j *intervalJobImpl) Status() error {
    43  	j.statusMutex.Lock()
    44  	defer j.statusMutex.Unlock()
    45  	return j.status
    46  }
    47  
    48  func (j *intervalJobImpl) setStatus(status error) {
    49  	j.statusMutex.Lock()
    50  	defer j.statusMutex.Unlock()
    51  	j.status = status
    52  }
    53  
    54  func (j *intervalJobImpl) Ready() bool {
    55  	j.readyMutex.Lock()
    56  	defer j.readyMutex.Unlock()
    57  	return j.ready
    58  }
    59  
    60  func (j *intervalJobImpl) setReady(ready bool) {
    61  	j.readyMutex.Lock()
    62  	defer j.readyMutex.Unlock()
    63  	j.ready = ready
    64  }
    65  
    66  func (j *intervalJobImpl) getWasFailed() bool {
    67  	j.jobMutex.Lock()
    68  	defer j.jobMutex.Unlock()
    69  	return j.wasFailed
    70  }
    71  
    72  func (j *intervalJobImpl) setWasFailed(wasFailed bool) {
    73  	j.jobMutex.Lock()
    74  	defer j.jobMutex.Unlock()
    75  	j.wasFailed = wasFailed
    76  }
    77  
    78  func (j *intervalJobImpl) getWasRestored() bool {
    79  	j.jobMutex.Lock()
    80  	defer j.jobMutex.Unlock()
    81  	return j.wasRestored
    82  }
    83  
    84  func (j *intervalJobImpl) setWasRestored(wasRestored bool) {
    85  	j.jobMutex.Lock()
    86  	defer j.jobMutex.Unlock()
    87  	j.wasRestored = wasRestored
    88  }
    89  
    90  func (j *intervalJobImpl) getExecutions() int {
    91  	j.jobMutex.Lock()
    92  	defer j.jobMutex.Unlock()
    93  	return j.executions
    94  }
    95  
    96  func (j *intervalJobImpl) incrementExecutions() {
    97  	j.jobMutex.Lock()
    98  	defer j.jobMutex.Unlock()
    99  	j.executions++
   100  }
   101  
   102  func (j *intervalJobImpl) clearExecutions() {
   103  	j.jobMutex.Lock()
   104  	defer j.jobMutex.Unlock()
   105  	j.executions = 0
   106  }
   107  
   108  func TestIntervalJob(t *testing.T) {
   109  	job := &intervalJobImpl{
   110  		name:        "IntervalJob",
   111  		runTime:     5 * time.Millisecond,
   112  		jobMutex:    &sync.Mutex{},
   113  		statusMutex: &sync.Mutex{},
   114  		readyMutex:  &sync.Mutex{},
   115  		ready:       false,
   116  	}
   117  
   118  	jobID, _ := RegisterIntervalJob(job, time.Millisecond)
   119  
   120  	statuses := []JobStatus{JobStatusRunning}
   121  	ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*10)
   122  	defer cancelFunc()
   123  
   124  	testDone := make(chan interface{})
   125  
   126  	go statusWaiter(ctx, t, statuses, jobID, testDone)
   127  
   128  	job.setReady(true)
   129  	<-testDone
   130  
   131  	assert.Nil(t, ctx.Err())
   132  	UnregisterJob(jobID)
   133  }