featurebase/tracker_test.go
Alan Bernstein bed03ba490 Update tests
2021-01-20 10:48:35 -06:00

81 lines
2.6 KiB
Go

// Copyright 2020 Pilosa Corp.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pilosa
import (
"fmt"
"testing"
"time"
)
func TestRingBuffer(t *testing.T) {
tests := []struct {
start int
count int
queries []string
}{
{start: 0, count: 0, queries: []string{}},
{start: 0, count: 1, queries: []string{"0"}},
{start: 0, count: 2, queries: []string{"0", "1"}},
{start: 0, count: 3, queries: []string{"0", "1", "2"}},
{start: 0, count: 4, queries: []string{"0", "1", "2", "3"}},
{start: 0, count: 5, queries: []string{"0", "1", "2", "3", "4"}},
{start: 1, count: 5, queries: []string{"1", "2", "3", "4", "5"}},
{start: 2, count: 5, queries: []string{"2", "3", "4", "5", "6"}},
{start: 3, count: 5, queries: []string{"3", "4", "5", "6", "7"}},
{start: 4, count: 5, queries: []string{"4", "5", "6", "7", "8"}},
{start: 0, count: 5, queries: []string{"5", "6", "7", "8", "9"}},
{start: 1, count: 5, queries: []string{"6", "7", "8", "9", "10"}},
{start: 2, count: 5, queries: []string{"7", "8", "9", "10", "11"}},
}
buffer := newRingBuffer(5)
for k := 0; k < len(tests); k++ {
if !(buffer.start == tests[k].start && buffer.count == tests[k].count) {
t.Fatalf("expected %d %d, found %d %d", tests[k].start, tests[k].count, buffer.start, buffer.count)
}
for n, q := range buffer.slice() {
if q.PQL != tests[k].queries[n] {
t.Fatalf("test[%d], buffer[%d] expected querystring '%s', found '%s'", k, n, tests[k].queries[n], q.PQL)
}
}
buffer.add(pastQuery{PQL: fmt.Sprintf("%d", k)})
}
}
func TestQueryTracker(t *testing.T) {
tracker := newQueryTracker(5)
defer tracker.Stop()
if queries := tracker.ActiveQueries(); len(queries) > 0 {
t.Fatalf("expected no active queries; found %v", queries)
}
qs := tracker.Start("test query", "test SQL", "node0", "i", time.Now())
var queries []ActiveQueryStatus
for len(queries) < 1 {
queries = tracker.ActiveQueries()
}
if len(queries) > 1 || queries[0].PQL != "test query" {
t.Fatalf("unexpected queries: %v", queries)
}
tracker.Finish(qs)
for len(queries) > 0 {
queries = tracker.ActiveQueries()
}
}