go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/cron/main_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package cron
     9  
    10  import (
    11  	"context"
    12  	"sync"
    13  	"time"
    14  )
    15  
    16  func inTimeDelta(a, b time.Time, delta time.Duration) bool {
    17  	actual := a.Sub(b)
    18  	if actual < 0 {
    19  		return -actual < delta
    20  	}
    21  	return actual < delta
    22  }
    23  
    24  func newLifecycleTest(action func(context.Context) error) *lifecycleTest {
    25  	return &lifecycleTest{
    26  		CompleteSignal: make(chan struct{}),
    27  		SuccessSignal:  make(chan struct{}),
    28  		BrokenSignal:   make(chan struct{}),
    29  		FixedSignal:    make(chan struct{}),
    30  		Action:         action,
    31  	}
    32  }
    33  
    34  type lifecycleTest struct {
    35  	sync.Mutex
    36  	Starts         int
    37  	Completes      int
    38  	Successes      int
    39  	Failures       int
    40  	CompleteSignal chan struct{}
    41  	SuccessSignal  chan struct{}
    42  	BrokenSignal   chan struct{}
    43  	FixedSignal    chan struct{}
    44  	Action         func(context.Context) error
    45  }
    46  
    47  func (job *lifecycleTest) Name() string { return "lifecycle-test" }
    48  func (job *lifecycleTest) Execute(ctx context.Context) error {
    49  	return job.Action(ctx)
    50  }
    51  func (job *lifecycleTest) Lifecycle() JobLifecycle {
    52  	return JobLifecycle{
    53  		OnBegin:    job.OnBegin,
    54  		OnComplete: job.OnComplete,
    55  		OnSuccess:  job.OnSuccess,
    56  		OnError:    job.OnError,
    57  		OnBroken:   job.OnBroken,
    58  		OnFixed:    job.OnFixed,
    59  	}
    60  }
    61  func (job *lifecycleTest) OnBegin(ctx context.Context) {
    62  	job.Starts++
    63  }
    64  func (job *lifecycleTest) OnError(ctx context.Context) {
    65  	job.Failures++
    66  }
    67  func (job *lifecycleTest) OnComplete(ctx context.Context) {
    68  	job.Lock()
    69  	defer job.Unlock()
    70  	close(job.CompleteSignal)
    71  	job.CompleteSignal = make(chan struct{})
    72  	job.Completes++
    73  }
    74  func (job *lifecycleTest) OnSuccess(ctx context.Context) {
    75  	job.Lock()
    76  	defer job.Unlock()
    77  	close(job.SuccessSignal)
    78  	job.SuccessSignal = make(chan struct{})
    79  	job.Successes++
    80  }
    81  func (job *lifecycleTest) OnBroken(ctx context.Context) {
    82  	job.Lock()
    83  	defer job.Unlock()
    84  	close(job.BrokenSignal)
    85  	job.BrokenSignal = make(chan struct{})
    86  }
    87  func (job *lifecycleTest) OnFixed(ctx context.Context) {
    88  	job.Lock()
    89  	defer job.Unlock()
    90  	close(job.FixedSignal)
    91  	job.BrokenSignal = make(chan struct{})
    92  }