From 5d089fec25f4d14f9f88c35a9adc5668faa6c5ad Mon Sep 17 00:00:00 2001 From: Ashutosh Narkar Date: Tue, 26 Sep 2023 15:32:19 -0700 Subject: [PATCH] server: Add test case for bundle update - query API handler scenario This test case exercises the scenario when a bundle is being written to the store (ie. active write transaction) and the OPA server handling a policy eval request at the same time. The former should not block the later. Fixes: #4792 Signed-off-by: Ashutosh Narkar --- server/server_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/server/server_test.go b/server/server_test.go index 59cb03f62aa..461a1b9ed46 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -2451,6 +2451,43 @@ func TestDataGetExplainFull(t *testing.T) { } } +func TestDataPostWithActiveStoreWriteTxn(t *testing.T) { + + f := newFixture(t) + + err := f.v1(http.MethodPut, "/policies/test", `package test + +p = [1, 2, 3, 4] { true }`, 200, "") + if err != nil { + t.Fatal(err) + } + + // open write transaction on the store and execute a query. + // Then check the query is processed + ctx := context.Background() + _ = storage.NewTransactionOrDie(ctx, f.server.store, storage.WriteParams) + + req := newReqV1(http.MethodPost, "/data/test/p", "") + f.reset() + f.server.Handler.ServeHTTP(f.recorder, req) + + var result types.DataResponseV1 + + if err := util.NewJSONDecoder(f.recorder.Body).Decode(&result); err != nil { + t.Fatalf("Unexpected JSON decode error: %v", err) + } + + var expected interface{} + + if err := util.UnmarshalJSON([]byte(`[1,2,3,4]`), &expected); err != nil { + panic(err) + } + + if result.Result == nil || !reflect.DeepEqual(*result.Result, expected) { + t.Fatalf("Expected %v but got: %v", expected, result.Result) + } +} + func TestDataPostExplain(t *testing.T) { f := newFixture(t)