|
1 | 1 | package coordination |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "sync" |
4 | 6 | "testing" |
| 7 | + "time" |
5 | 8 |
|
6 | 9 | "github.com/stretchr/testify/require" |
| 10 | + "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Coordination" |
| 11 | + "go.uber.org/mock/gomock" |
| 12 | + |
| 13 | + "github.com/ydb-platform/ydb-go-sdk/v3/trace" |
7 | 14 | ) |
8 | 15 |
|
| 16 | +func TestReceiveLoopUpdatesLastGoodResponseTimeBeforeSessionStarted(t *testing.T) { |
| 17 | + ctrl := gomock.NewController(t) |
| 18 | + client := NewMockCoordinationService_SessionClient(ctrl) |
| 19 | + streamCtx, cancelStream := context.WithCancel(context.Background()) |
| 20 | + startedResponse := &Ydb_Coordination.SessionResponse_SessionStarted{ |
| 21 | + SessionId: 42, |
| 22 | + } |
| 23 | + recvReturned := make(chan struct{}) |
| 24 | + |
| 25 | + firstRecv := client.EXPECT().Recv().DoAndReturn(func() (*Ydb_Coordination.SessionResponse, error) { |
| 26 | + close(recvReturned) |
| 27 | + |
| 28 | + return &Ydb_Coordination.SessionResponse{ |
| 29 | + Response: &Ydb_Coordination.SessionResponse_SessionStarted_{ |
| 30 | + SessionStarted: startedResponse, |
| 31 | + }, |
| 32 | + }, nil |
| 33 | + }) |
| 34 | + secondRecv := client.EXPECT().Recv().DoAndReturn(func() (*Ydb_Coordination.SessionResponse, error) { |
| 35 | + <-streamCtx.Done() |
| 36 | + |
| 37 | + return nil, streamCtx.Err() |
| 38 | + }) |
| 39 | + gomock.InOrder(firstRecv.Call, secondRecv.Call) |
| 40 | + |
| 41 | + s := &session{ |
| 42 | + trace: &trace.Coordination{}, |
| 43 | + } |
| 44 | + sessionStarted := make(chan *Ydb_Coordination.SessionResponse_SessionStarted, 1) |
| 45 | + sessionStopped := make(chan *Ydb_Coordination.SessionResponse_SessionStopped, 1) |
| 46 | + wg := sync.WaitGroup{} |
| 47 | + wg.Add(1) |
| 48 | + s.mutex.Lock() |
| 49 | + go s.receiveLoop(&wg, client, cancelStream, sessionStarted, sessionStopped) |
| 50 | + |
| 51 | + <-recvReturned |
| 52 | + var start *Ydb_Coordination.SessionResponse_SessionStarted |
| 53 | + select { |
| 54 | + case start = <-sessionStarted: |
| 55 | + case <-time.After(100 * time.Millisecond): //nolint:mnd |
| 56 | + } |
| 57 | + publishedBeforeTimestamp := start != nil |
| 58 | + s.mutex.Unlock() |
| 59 | + if start == nil { |
| 60 | + start = <-sessionStarted |
| 61 | + } |
| 62 | + |
| 63 | + cancelStream() |
| 64 | + wg.Wait() |
| 65 | + require.False(t, publishedBeforeTimestamp, "session started was published before the keep-alive timestamp update") |
| 66 | + require.Same(t, startedResponse, start) |
| 67 | + require.False(t, s.getLastGoodResponseTime().IsZero()) |
| 68 | +} |
| 69 | + |
9 | 70 | func TestNewProtectionKey(t *testing.T) { |
10 | 71 | key1 := newProtectionKey() |
11 | 72 | require.NotNil(t, key1) |
|
0 commit comments