|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package cursor |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestCursor_IsNew(t *testing.T) { |
| 27 | + t.Run("true if key is not in store", func(t *testing.T) { |
| 28 | + store := testOpenStore(t, "test", createSampleStore(t, nil)) |
| 29 | + defer store.Release() |
| 30 | + |
| 31 | + cursor := makeCursor(store, store.Get("test::key")) |
| 32 | + require.True(t, cursor.IsNew()) |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("true if key is in store but without cursor value", func(t *testing.T) { |
| 36 | + store := testOpenStore(t, "test", createSampleStore(t, map[string]state{ |
| 37 | + "test::key": {Cursor: nil}, |
| 38 | + })) |
| 39 | + defer store.Release() |
| 40 | + |
| 41 | + cursor := makeCursor(store, store.Get("test::key")) |
| 42 | + require.True(t, cursor.IsNew()) |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("false if key with cursor value is in persistent store", func(t *testing.T) { |
| 46 | + store := testOpenStore(t, "test", createSampleStore(t, map[string]state{ |
| 47 | + "test::key": {Cursor: "test"}, |
| 48 | + })) |
| 49 | + defer store.Release() |
| 50 | + |
| 51 | + cursor := makeCursor(store, store.Get("test::key")) |
| 52 | + require.False(t, cursor.IsNew()) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("false if key with cursor value is in memory store only", func(t *testing.T) { |
| 56 | + store := testOpenStore(t, "test", createSampleStore(t, map[string]state{ |
| 57 | + "test::key": {Cursor: nil}, |
| 58 | + })) |
| 59 | + defer store.Release() |
| 60 | + |
| 61 | + res := store.Get("test::key") |
| 62 | + op, err := createUpdateOp(store, res, "test-state-update") |
| 63 | + require.NoError(t, err) |
| 64 | + defer op.done(1) |
| 65 | + |
| 66 | + cursor := makeCursor(store, res) |
| 67 | + require.False(t, cursor.IsNew()) |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +func TestCursor_Unpack(t *testing.T) { |
| 72 | + t.Run("nothing to unpack if key is new", func(t *testing.T) { |
| 73 | + store := testOpenStore(t, "test", createSampleStore(t, nil)) |
| 74 | + defer store.Release() |
| 75 | + |
| 76 | + var st string |
| 77 | + cursor := makeCursor(store, store.Get("test::key")) |
| 78 | + |
| 79 | + require.NoError(t, cursor.Unpack(&st)) |
| 80 | + require.Equal(t, "", st) |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("unpack fails if types are not compatible", func(t *testing.T) { |
| 84 | + store := testOpenStore(t, "test", createSampleStore(t, map[string]state{ |
| 85 | + "test::key": {Cursor: "test"}, |
| 86 | + })) |
| 87 | + defer store.Release() |
| 88 | + |
| 89 | + var st struct{ A uint } |
| 90 | + cursor := makeCursor(store, store.Get("test::key")) |
| 91 | + require.Error(t, cursor.Unpack(&st)) |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("unpack from state in persistent store", func(t *testing.T) { |
| 95 | + store := testOpenStore(t, "test", createSampleStore(t, map[string]state{ |
| 96 | + "test::key": {Cursor: "test"}, |
| 97 | + })) |
| 98 | + defer store.Release() |
| 99 | + |
| 100 | + var st string |
| 101 | + cursor := makeCursor(store, store.Get("test::key")) |
| 102 | + |
| 103 | + require.NoError(t, cursor.Unpack(&st)) |
| 104 | + require.Equal(t, "test", st) |
| 105 | + }) |
| 106 | + |
| 107 | + t.Run("unpack from in memory state if updates are pending", func(t *testing.T) { |
| 108 | + store := testOpenStore(t, "test", createSampleStore(t, map[string]state{ |
| 109 | + "test::key": {Cursor: "test"}, |
| 110 | + })) |
| 111 | + defer store.Release() |
| 112 | + |
| 113 | + res := store.Get("test::key") |
| 114 | + op, err := createUpdateOp(store, res, "test-state-update") |
| 115 | + require.NoError(t, err) |
| 116 | + defer op.done(1) |
| 117 | + |
| 118 | + var st string |
| 119 | + cursor := makeCursor(store, store.Get("test::key")) |
| 120 | + |
| 121 | + require.NoError(t, cursor.Unpack(&st)) |
| 122 | + require.Equal(t, "test-state-update", st) |
| 123 | + }) |
| 124 | +} |
0 commit comments