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

     1  package jobs
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestJobLocks(t *testing.T) {
    12  	failJob := &intervalJobImpl{
    13  		name:        "FailedIntervalJob",
    14  		runTime:     50 * time.Millisecond,
    15  		ready:       true,
    16  		jobMutex:    &sync.Mutex{},
    17  		statusMutex: &sync.Mutex{},
    18  		readyMutex:  &sync.Mutex{},
    19  	}
    20  	jobID, _ := RegisterIntervalJob(failJob, 10*time.Millisecond)
    21  
    22  	// The job is running, waiting 120 milliseconds before locking
    23  	time.Sleep(120 * time.Millisecond)
    24  	JobLock(jobID) // Lock the job
    25  	curExecutions := failJob.executions
    26  
    27  	// sleep another 120 milliseconds and validate the job has not continued
    28  	time.Sleep(120 * time.Millisecond)
    29  	newExecutions := failJob.executions
    30  	assert.Equal(t, curExecutions, newExecutions, "The job ran more executions after locking")
    31  
    32  	// Unlock the job then sleep another 120 milliseconds to check more executions have happened
    33  	JobUnlock(jobID) // Unlock the job
    34  	time.Sleep(120 * time.Millisecond)
    35  	newExecutions = failJob.getExecutions()
    36  	assert.Greater(t, newExecutions, curExecutions, "The job did not run more executions after unlocking")
    37  
    38  	// Run test again using the jobExecution to get the locks
    39  	// Get the job
    40  	jobExecution := GetJob(jobID)
    41  
    42  	// The job is running, waiting 120 milliseconds before locking
    43  	time.Sleep(120 * time.Millisecond)
    44  	jobExecution.Lock() // Lock the job
    45  	curExecutions = failJob.executions
    46  
    47  	// sleep another 120 milliseconds and validate the job has not continued
    48  	time.Sleep(120 * time.Millisecond)
    49  	newExecutions = failJob.executions
    50  	assert.Equal(t, curExecutions, newExecutions, "The job ran more executions after locking")
    51  
    52  	// Unlock the job then sleep another 120 milliseconds to check more executions have happened
    53  	jobExecution.Unlock() // Unlock the job
    54  	time.Sleep(120 * time.Millisecond)
    55  	newExecutions = failJob.getExecutions()
    56  	assert.Greater(t, newExecutions, curExecutions, "The job did not run more executions after unlocking")
    57  }