|
| 1 | +/* |
| 2 | + * Copyright 2021 Layotto Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package in_memory |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + |
| 24 | + "mosn.io/layotto/components/sequencer" |
| 25 | +) |
| 26 | + |
| 27 | +func TestNew(t *testing.T) { |
| 28 | + s := NewInMemorySequencer() |
| 29 | + |
| 30 | + assert.NotNil(t, s) |
| 31 | +} |
| 32 | + |
| 33 | +func TestInit(t *testing.T) { |
| 34 | + s := NewInMemorySequencer() |
| 35 | + assert.NotNil(t, s) |
| 36 | + |
| 37 | + err := s.Init(sequencer.Configuration{}) |
| 38 | + assert.NoError(t, err) |
| 39 | +} |
| 40 | + |
| 41 | +func TestGetNextId(t *testing.T) { |
| 42 | + s := NewInMemorySequencer() |
| 43 | + assert.NotNil(t, s) |
| 44 | + |
| 45 | + err := s.Init(sequencer.Configuration{}) |
| 46 | + assert.NoError(t, err) |
| 47 | + |
| 48 | + var resp *sequencer.GetNextIdResponse |
| 49 | + resp, err = s.GetNextId(&sequencer.GetNextIdRequest{Key: "666"}) |
| 50 | + assert.NoError(t, err) |
| 51 | + assert.Equal(t, int64(1), resp.NextId) |
| 52 | + |
| 53 | + resp, err = s.GetNextId(&sequencer.GetNextIdRequest{Key: "666"}) |
| 54 | + assert.NoError(t, err) |
| 55 | + assert.Equal(t, int64(2), resp.NextId) |
| 56 | + |
| 57 | + resp, err = s.GetNextId(&sequencer.GetNextIdRequest{Key: "777"}) |
| 58 | + assert.NoError(t, err) |
| 59 | + assert.Equal(t, int64(1), resp.NextId) |
| 60 | +} |
| 61 | + |
| 62 | +func TestGetSegment(t *testing.T) { |
| 63 | + s := NewInMemorySequencer() |
| 64 | + assert.NotNil(t, s) |
| 65 | + |
| 66 | + err := s.Init(sequencer.Configuration{}) |
| 67 | + assert.NoError(t, err) |
| 68 | + |
| 69 | + var resp *sequencer.GetSegmentResponse |
| 70 | + var res bool |
| 71 | + res, resp, err = s.GetSegment(&sequencer.GetSegmentRequest{Key: "666", Size: 5}) |
| 72 | + assert.NoError(t, err) |
| 73 | + assert.Equal(t, int64(1), resp.From) |
| 74 | + assert.Equal(t, int64(5), resp.To) |
| 75 | + assert.True(t, res) |
| 76 | + |
| 77 | + res, resp, err = s.GetSegment(&sequencer.GetSegmentRequest{Key: "666", Size: 5}) |
| 78 | + assert.NoError(t, err) |
| 79 | + assert.Equal(t, int64(6), resp.From) |
| 80 | + assert.Equal(t, int64(10), resp.To) |
| 81 | + assert.True(t, res) |
| 82 | + |
| 83 | + res, resp, err = s.GetSegment(&sequencer.GetSegmentRequest{Key: "777", Size: 5}) |
| 84 | + assert.NoError(t, err) |
| 85 | + assert.Equal(t, int64(1), resp.From) |
| 86 | + assert.Equal(t, int64(5), resp.To) |
| 87 | + assert.True(t, res) |
| 88 | + |
| 89 | +} |
0 commit comments