gitlab.com/ignitionrobotics/web/ign-go@v1.0.0-rc4/scheduler/scheduler_test.go (about)

     1  package scheduler
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  // TestSingleton tests the correct type of the singleton
    11  func TestSingleton(t *testing.T) {
    12  	s1 := GetInstance()
    13  	s2 := GetInstance()
    14  	assert.IsType(t, Scheduler{}, *s1)
    15  	assert.IsType(t, Scheduler{}, *s2)
    16  	assert.Equal(t, s1, s2)
    17  }
    18  
    19  // TestDoIn tests the changes on data when using DoIn method.
    20  func TestDoIn(t *testing.T) {
    21  	s := GetInstance()
    22  	b := false
    23  	var wg sync.WaitGroup
    24  
    25  	assert.Equal(t, false, b)
    26  
    27  	wg.Add(1)
    28  	s.DoIn(func() {
    29  		b = true
    30  		wg.Done()
    31  	}, 1)
    32  
    33  	wg.Wait()
    34  
    35  	assert.Equal(t, true, b)
    36  }
    37  
    38  // TestDoEvery tests the changes on data when using DoEvery method.
    39  func TestDoEvery(t *testing.T) {
    40  	s := GetInstance()
    41  	count := 1
    42  	var wg sync.WaitGroup
    43  
    44  	assert.Equal(t, 1, count)
    45  
    46  	wg.Add(2)
    47  	s.DoEvery(func() {
    48  		count = count + 1
    49  		wg.Done()
    50  	}, 1)
    51  
    52  	wg.Wait()
    53  
    54  	assert.Equal(t, 3, count)
    55  }
    56  
    57  // TestDoAt tests the changes on data when using DoAt method.
    58  func TestDoAt(t *testing.T) {
    59  	s := GetInstance()
    60  	b := false
    61  	var wg sync.WaitGroup
    62  
    63  	assert.Equal(t, false, b)
    64  
    65  	wg.Add(1)
    66  	s.DoAt(func() {
    67  		b = true
    68  		wg.Done()
    69  	}, time.Now().Add(time.Second*3))
    70  
    71  	wg.Wait()
    72  
    73  	assert.Equal(t, true, b)
    74  }