|
| 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 | + "context" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + |
| 27 | + "github.com/elastic/beats/v7/libbeat/beat" |
| 28 | + pubtest "github.com/elastic/beats/v7/libbeat/publisher/testing" |
| 29 | +) |
| 30 | + |
| 31 | +func TestPublish(t *testing.T) { |
| 32 | + t.Run("event with cursor state creates update operation", func(t *testing.T) { |
| 33 | + store := testOpenStore(t, "test", createSampleStore(t, nil)) |
| 34 | + defer store.Release() |
| 35 | + cursor := makeCursor(store, store.Get("test::key")) |
| 36 | + |
| 37 | + var actual beat.Event |
| 38 | + client := &pubtest.FakeClient{ |
| 39 | + PublishFunc: func(event beat.Event) { actual = event }, |
| 40 | + } |
| 41 | + publisher := cursorPublisher{nil, client, &cursor} |
| 42 | + publisher.Publish(beat.Event{}, "test") |
| 43 | + |
| 44 | + require.NotNil(t, actual.Private) |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("event without cursor creates no update operation", func(t *testing.T) { |
| 48 | + store := testOpenStore(t, "test", createSampleStore(t, nil)) |
| 49 | + defer store.Release() |
| 50 | + cursor := makeCursor(store, store.Get("test::key")) |
| 51 | + |
| 52 | + var actual beat.Event |
| 53 | + client := &pubtest.FakeClient{ |
| 54 | + PublishFunc: func(event beat.Event) { actual = event }, |
| 55 | + } |
| 56 | + publisher := cursorPublisher{nil, client, &cursor} |
| 57 | + publisher.Publish(beat.Event{}, nil) |
| 58 | + require.Nil(t, actual.Private) |
| 59 | + }) |
| 60 | + |
| 61 | + t.Run("publish returns error if context has been cancelled", func(t *testing.T) { |
| 62 | + ctx, cancel := context.WithCancel(context.TODO()) |
| 63 | + cancel() |
| 64 | + |
| 65 | + store := testOpenStore(t, "test", createSampleStore(t, nil)) |
| 66 | + defer store.Release() |
| 67 | + cursor := makeCursor(store, store.Get("test::key")) |
| 68 | + |
| 69 | + publisher := cursorPublisher{ctx, &pubtest.FakeClient{}, &cursor} |
| 70 | + err := publisher.Publish(beat.Event{}, nil) |
| 71 | + require.Equal(t, context.Canceled, err) |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +func TestOp_Execute(t *testing.T) { |
| 76 | + t.Run("applying final op marks the key as finished", func(t *testing.T) { |
| 77 | + store := testOpenStore(t, "test", createSampleStore(t, nil)) |
| 78 | + defer store.Release() |
| 79 | + res := store.Get("test::key") |
| 80 | + |
| 81 | + // create op and release resource. The 'resource' must still be active |
| 82 | + op := mustCreateUpdateOp(t, store, res, "test-updated-cursor-state") |
| 83 | + res.Release() |
| 84 | + require.False(t, res.Finished()) |
| 85 | + |
| 86 | + // this was the last op, the resource should become inactive |
| 87 | + op.Execute(1) |
| 88 | + require.True(t, res.Finished()) |
| 89 | + |
| 90 | + // validate state: |
| 91 | + inSyncCursor := storeInSyncSnapshot(store)["test::key"].Cursor |
| 92 | + inMemCursor := storeMemorySnapshot(store)["test::key"].Cursor |
| 93 | + want := "test-updated-cursor-state" |
| 94 | + assert.Equal(t, want, inSyncCursor) |
| 95 | + assert.Equal(t, want, inMemCursor) |
| 96 | + }) |
| 97 | + |
| 98 | + t.Run("acking multiple ops applies the latest update and marks key as finished", func(t *testing.T) { |
| 99 | + // when acking N events, intermediate updates are dropped in favor of the latest update operation. |
| 100 | + // This test checks that the resource is correctly marked as finished. |
| 101 | + |
| 102 | + store := testOpenStore(t, "test", createSampleStore(t, nil)) |
| 103 | + defer store.Release() |
| 104 | + res := store.Get("test::key") |
| 105 | + |
| 106 | + // create update operations and release resource. The 'resource' must still be active |
| 107 | + mustCreateUpdateOp(t, store, res, "test-updated-cursor-state-dropped") |
| 108 | + op := mustCreateUpdateOp(t, store, res, "test-updated-cursor-state-final") |
| 109 | + res.Release() |
| 110 | + require.False(t, res.Finished()) |
| 111 | + |
| 112 | + // this was the last op, the resource should become inactive |
| 113 | + op.Execute(2) |
| 114 | + require.True(t, res.Finished()) |
| 115 | + |
| 116 | + // validate state: |
| 117 | + inSyncCursor := storeInSyncSnapshot(store)["test::key"].Cursor |
| 118 | + inMemCursor := storeMemorySnapshot(store)["test::key"].Cursor |
| 119 | + want := "test-updated-cursor-state-final" |
| 120 | + assert.Equal(t, want, inSyncCursor) |
| 121 | + assert.Equal(t, want, inMemCursor) |
| 122 | + }) |
| 123 | + |
| 124 | + t.Run("ACK only subset of pending ops will only update up to ACKed state", func(t *testing.T) { |
| 125 | + // when acking N events, intermediate updates are dropped in favor of the latest update operation. |
| 126 | + // This test checks that the resource is correctly marked as finished. |
| 127 | + |
| 128 | + store := testOpenStore(t, "test", createSampleStore(t, nil)) |
| 129 | + defer store.Release() |
| 130 | + res := store.Get("test::key") |
| 131 | + |
| 132 | + // create update operations and release resource. The 'resource' must still be active |
| 133 | + op1 := mustCreateUpdateOp(t, store, res, "test-updated-cursor-state-intermediate") |
| 134 | + op2 := mustCreateUpdateOp(t, store, res, "test-updated-cursor-state-final") |
| 135 | + res.Release() |
| 136 | + require.False(t, res.Finished()) |
| 137 | + |
| 138 | + defer op2.done(1) // cleanup after test |
| 139 | + |
| 140 | + // this was the intermediate op, the resource should still be active |
| 141 | + op1.Execute(1) |
| 142 | + require.False(t, res.Finished()) |
| 143 | + |
| 144 | + // validate state (in memory state is always up to data to most recent update): |
| 145 | + inSyncCursor := storeInSyncSnapshot(store)["test::key"].Cursor |
| 146 | + inMemCursor := storeMemorySnapshot(store)["test::key"].Cursor |
| 147 | + assert.Equal(t, "test-updated-cursor-state-intermediate", inSyncCursor) |
| 148 | + assert.Equal(t, "test-updated-cursor-state-final", inMemCursor) |
| 149 | + }) |
| 150 | +} |
| 151 | + |
| 152 | +func mustCreateUpdateOp(t *testing.T, store *store, resource *resource, updates interface{}) *updateOp { |
| 153 | + op, err := createUpdateOp(store, resource, updates) |
| 154 | + if err != nil { |
| 155 | + t.Fatalf("Failed to create update op: %v", err) |
| 156 | + } |
| 157 | + return op |
| 158 | +} |
0 commit comments