-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathservice_test.go
57 lines (45 loc) · 1.62 KB
/
service_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package service
import (
"context"
"os"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/acronis/go-appkit/log/logtest"
)
func TestService_Start(t *testing.T) {
logRecorder := logtest.NewRecorder()
var runningCounter int32
mockUnit := newMockUnit("srv", &runningCounter, false)
service := New(logRecorder, mockUnit)
go func() {
require.NoError(t, service.Start())
}()
require.NoError(t, waitTrue(func() bool { return atomic.LoadInt32(&runningCounter) == 1 }, time.Second*3))
require.Equal(t, 1, mockUnit.mustRegisterMetricsCalled)
require.Equal(t, 1, mockUnit.startCalled)
service.Signals <- os.Interrupt // Sending SIGINT signal to the service.
require.NoError(t, waitTrue(func() bool { return atomic.LoadInt32(&runningCounter) == 0 }, time.Second*3))
require.Equal(t, 1, mockUnit.unregisterMetricsCalled)
require.Equal(t, 1, mockUnit.stopCalled)
require.Equal(t, 1, mockUnit.stopGracefullyCalled)
}
func TestService_StartContext(t *testing.T) {
ctx, ctxCancel := context.WithCancel(context.Background())
logRecorder := logtest.NewRecorder()
var runningCounter int32
mockUnit := newMockUnit("srv", &runningCounter, false)
service := New(logRecorder, mockUnit)
go func() {
require.NoError(t, service.StartContext(ctx))
}()
require.NoError(t, waitTrue(func() bool { return atomic.LoadInt32(&runningCounter) == 1 }, time.Second*3))
ctxCancel()
require.NoError(t, waitTrue(func() bool { return atomic.LoadInt32(&runningCounter) == 0 }, time.Second*3))
require.Equal(t, 1, mockUnit.stopGracefullyCalled)
}