featurebase/test/field.go
Seebs cecaf99ee4 testhook: leak auditing infrastructure
The testhook/ package provides an easy way to set up multiple
hooks to run before/after tests are run.

The audit hooks track open and closes of storage backends,
files, indexes, and holders, for example. A tempdir wrapper
creates temporary directories which are automatically cleaned up
when the test ends. Any kind of resource creation that
should be closed at test conclusion can be tracked. We
will complain at the end of the TestMain if resources are
leaking.

Leaks under go1.13:

We use a wrapper function which is a no-op for go 1.13, but actually
calls testing.TB.Cleanup in go1.14, so we can still build with 1.13 even though
tests will leak files all over the place there. Because of this,
don't run the testhook tests when using 1.13, as they'll always fail.

- the test/pilosa.go http client now times out after 10 seconds
to help diagnose hung server situations.

- Makefile targets added to get better progress reports.
2020-08-24 11:26:39 -05:00

96 lines
2.5 KiB
Go

// Copyright 2017 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 test
import (
"os"
"testing"
"github.com/pilosa/pilosa/v2"
"github.com/pilosa/pilosa/v2/testhook"
)
// Field represents a test wrapper for pilosa.Field.
type Field struct {
*pilosa.Field
}
// newField returns a new instance of Field d/0.
func newField(tb testing.TB, opts pilosa.FieldOption) *Field {
path, err := testhook.TempDir(tb, "pilosa-field-")
if err != nil {
panic(err)
}
field, err := pilosa.NewField(pilosa.NewHolder(pilosa.DefaultPartitionN), path, "i", "f", opts)
if err != nil {
panic(err)
}
return &Field{Field: field}
}
// mustOpenField returns a new, opened field at a temporary path. Panic on error.
func mustOpenField(tb testing.TB, opts pilosa.FieldOption) *Field {
f := newField(tb, opts)
if err := f.Open(); err != nil {
panic(err)
}
return f
}
// close closes the field and removes the underlying data.
func (f *Field) close() error { // nolint: unparam
defer os.RemoveAll(f.Path())
return f.Field.Close()
}
// reopen closes the index and reopens it.
func (f *Field) reopen() error {
var err error
if err := f.Field.Close(); err != nil {
return err
}
path, index, name := f.Path(), f.Index(), f.Name()
f.Field, err = pilosa.NewField(pilosa.NewHolder(pilosa.DefaultPartitionN), path, index, name, pilosa.OptFieldTypeDefault())
if err != nil {
return err
}
if err := f.Open(); err != nil {
return err
}
return nil
}
// Ensure field can set its cache
func TestField_SetCacheSize(t *testing.T) {
f := mustOpenField(t, pilosa.OptFieldTypeDefault())
defer f.close()
cacheSize := uint32(100)
// Set & retrieve field cache size.
if err := f.SetCacheSize(cacheSize); err != nil {
t.Fatal(err)
} else if q := f.CacheSize(); q != cacheSize {
t.Fatalf("unexpected field cache size: %d", q)
}
// Reload field and verify that it is persisted.
if err := f.reopen(); err != nil {
t.Fatal(err)
} else if q := f.CacheSize(); q != cacheSize {
t.Fatalf("unexpected field cache size (reopen): %d", q)
}
}