refactor safeCopy to pure function and add unit test

This commit is contained in:
reesporte 2021-11-17 08:51:28 -06:00
parent 7390ae072d
commit f8e93871c0
2 changed files with 14 additions and 2 deletions

View file

@ -254,7 +254,7 @@ func (e *executor) Execute(ctx context.Context, index string, q *pql.Query, shar
}
}
// Must copy out of Tx data before Commiting, because it will become invalid afterwards.
respSafeNoTxData := e.safeCopy(resp)
respSafeNoTxData := safeCopy(resp)
// Commit transactions if writing; else let the defer grp.Abort do the rollbacks.
if needWriteTxn {
@ -267,7 +267,7 @@ func (e *executor) Execute(ctx context.Context, index string, q *pql.Query, shar
// safeCopy copies everything in resp that has Bitmap material,
// to avoid anything coming from the mmap-ed Tx storage.
func (e *executor) safeCopy(resp QueryResponse) (out QueryResponse) {
func safeCopy(resp QueryResponse) (out QueryResponse) {
out = QueryResponse{
Err: resp.Err, // error
Profile: resp.Profile, // *tracing.Profile

View file

@ -475,3 +475,15 @@ func TestGetSorter(t *testing.T) {
})
}
}
func TestExecutorSafeCopyDistinctTimestamp(t *testing.T) {
result := DistinctTimestamp{Values: []string{"test", "test"}, Name: "test"}
results := make([]interface{}, 1)
results[0] = result
response := QueryResponse{Results: results, Err: nil, Profile: nil}
copied := safeCopy(response)
if !reflect.DeepEqual(copied.Results, response.Results) {
t.Fatalf("Did not copy results. got %+v, want %+v", copied.Results, response.Results)
}
}