Skip to content

Commit

Permalink
fix: broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
buraksezer committed Dec 15, 2020
1 parent 46e6e5b commit 2875c0d
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 57 deletions.
2 changes: 1 addition & 1 deletion cmd/olricd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (s *Olricd) waitForInterrupt() {

// Start starts a new olricd server instance and blocks until the server is closed.
func (s *Olricd) Start() error {
s.log.Printf("[olricd] pid: %d has been started", os.Getpid())
// Wait for SIGTERM or SIGINT
go s.waitForInterrupt()

Expand All @@ -83,7 +84,6 @@ func (s *Olricd) Start() error {
return err
}
s.db = db
s.log.Printf("[olricd] pid: %d has been started on %s:%d", os.Getpid(), s.config.BindAddr, s.config.BindPort)
s.errgr.Go(func() error {
if err = s.db.Start(); err != nil {
s.log.Printf("[olricd] Failed to run Olric: %v", err)
Expand Down
31 changes: 3 additions & 28 deletions dmap_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@ package olric
import (
"context"
"github.com/buraksezer/olric/config"
"strings"
"testing"
"time"

"github.com/buraksezer/olric/internal/protocol"
"github.com/buraksezer/olric/internal/transport"
"github.com/buraksezer/olric/query"
"github.com/vmihailenco/msgpack"
"strings"
"testing"
)

func TestDMap_QueryOnKeyStandalone(t *testing.T) {
c := testSingleReplicaConfig()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
c.Started = func() {
cancel()
}

db, err := newDB(c)
if err != nil {
t.Fatalf("Expected nil. Got: %v", err)
Expand All @@ -44,7 +37,6 @@ func TestDMap_QueryOnKeyStandalone(t *testing.T) {
db.log.V(2).Printf("[ERROR] Failed to shutdown Olric: %v", err)
}
}()
<-ctx.Done()

dm, err := db.NewDMap("mydmap")
if err != nil {
Expand Down Expand Up @@ -159,11 +151,6 @@ func TestDMap_QueryOnKeyCluster(t *testing.T) {

func TestDMap_QueryOnKeyIgnoreValues(t *testing.T) {
c := testSingleReplicaConfig()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
c.Started = func() {
cancel()
}

db, err := newDB(c)
if err != nil {
t.Fatalf("Expected nil. Got: %v", err)
Expand All @@ -174,7 +161,6 @@ func TestDMap_QueryOnKeyIgnoreValues(t *testing.T) {
db.log.V(2).Printf("[ERROR] Failed to shutdown Olric: %v", err)
}
}()
<-ctx.Done()

dm, err := db.NewDMap("mydmap")
if err != nil {
Expand Down Expand Up @@ -291,11 +277,6 @@ func TestDMap_IteratorCluster(t *testing.T) {

func TestDMap_Query(t *testing.T) {
c := testSingleReplicaConfig()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
c.Started = func() {
cancel()
}

db, err := newDB(c)
if err != nil {
t.Fatalf("Expected nil. Got: %v", err)
Expand All @@ -306,7 +287,7 @@ func TestDMap_Query(t *testing.T) {
db.log.V(2).Printf("[ERROR] Failed to shutdown Olric: %v", err)
}
}()
<-ctx.Done()

dm, err := db.NewDMap("mydmap")
if err != nil {
t.Fatalf("Expected nil. Got: %v", err)
Expand Down Expand Up @@ -375,11 +356,6 @@ func TestDMap_Query(t *testing.T) {

func TestDMap_QueryEndOfKeySpace(t *testing.T) {
c := testSingleReplicaConfig()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
c.Started = func() {
cancel()
}

db, err := newDB(c)
if err != nil {
t.Fatalf("Expected nil. Got: %v", err)
Expand All @@ -390,7 +366,6 @@ func TestDMap_QueryEndOfKeySpace(t *testing.T) {
db.log.V(2).Printf("[ERROR] Failed to shutdown Olric: %v", err)
}
}()
<-ctx.Done()

q := query.M{
"$onKey": query.M{
Expand Down
17 changes: 13 additions & 4 deletions dmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ package olric

import (
"context"
"errors"
"fmt"
"github.com/buraksezer/olric/internal/kvstore"
"net"
"strconv"
"sync"
"testing"
"time"

"github.com/buraksezer/olric/config"
"github.com/buraksezer/olric/internal/kvstore"
"github.com/hashicorp/memberlist"
)

Expand Down Expand Up @@ -121,9 +122,9 @@ func newDB(c *config.Config, peers ...*Olric) (*Olric, error) {
return nil, err
}

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
c.Started = func() {
defer cancel()
cancel()
}

db, err := New(c)
Expand All @@ -139,7 +140,15 @@ func newDB(c *config.Config, peers ...*Olric) (*Olric, error) {
db.log.V(2).Printf("[ERROR] Failed to start Olric node: %s", serr)
}
}()
<-ctx.Done()

select {
case <-time.After(11 * time.Second):
return nil, errors.New("node cannot be started in 10 second")
case <-ctx.Done():
if ctx.Err() != context.Canceled {
return nil, fmt.Errorf("context returned an error: %v", ctx.Err())
}
}
return db, nil
}

Expand Down
8 changes: 7 additions & 1 deletion internal/kvstore/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package kvstore

import "encoding/binary"
import (
"encoding/binary"

"github.com/buraksezer/olric/pkg/storage"
)

// In-memory layout for an entry:
//
Expand All @@ -28,6 +32,8 @@ type Entry struct {
value []byte
}

var _ storage.Entry = (*Entry)(nil)

func NewEntry() *Entry {
return &Entry{}
}
Expand Down
7 changes: 6 additions & 1 deletion internal/kvstore/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package kvstore

import (
"errors"
"log"
"regexp"

"github.com/buraksezer/olric/pkg/storage"
Expand All @@ -39,6 +40,8 @@ type KVStore struct {
config *storage.Config
}

var _ storage.Engine = (*KVStore)(nil)

func DefaultConfig() *storage.Config {
options := storage.NewConfig(nil)
options.Add("tableSize", defaultTableSize)
Expand All @@ -49,6 +52,8 @@ func (kv *KVStore) SetConfig(c *storage.Config) {
kv.config = c
}

func (kv *KVStore) SetLogger(l *log.Logger) {}

func (kv *KVStore) Start() error {
if kv.config == nil {
return errors.New("config cannot be nil")
Expand All @@ -69,7 +74,7 @@ func (kv *KVStore) Fork(c *storage.Config) (storage.Engine, error) {
config: c,
}
t := newTable(size.(int))
child.tables = append(kv.tables, t)
child.tables = append(child.tables, t)
return child, nil
}

Expand Down
66 changes: 65 additions & 1 deletion internal/kvstore/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ func bval(i int) []byte {
func testKVStore() (storage.Engine, error) {
kv := &KVStore{}
kv.SetConfig(DefaultConfig())
return kv.Fork(nil)
child, err := kv.Fork(nil)
if err != nil {
return nil, err
}

err = child.Start()
if err != nil {
return nil, err
}
return child, nil
}

func Test_Put(t *testing.T) {
Expand Down Expand Up @@ -577,3 +586,58 @@ func TestStorage_MatchOnKey(t *testing.T) {
t.Fatalf("Expected count is 50. Got: %d", count)
}
}

func Test_Fork(t *testing.T) {
s, err := testKVStore()
if err != nil {
t.Fatalf("Expected nil. Got %v", err)
}
timestamp := time.Now().UnixNano()
for i := 0; i < 10; i++ {
entry := &Entry{
key: bkey(i),
ttl: int64(i),
value: bval(i),
timestamp: timestamp,
}
hkey := xxhash.Sum64([]byte(entry.Key()))
err := s.Put(hkey, entry)
if err != nil {
t.Fatalf("Expected nil. Got %v", err)
}
}

child, err := s.Fork(nil)
if err != nil {
t.Fatalf("Expected nil. Got %v", err)
}

for i := 0; i < 100; i++ {
hkey := xxhash.Sum64([]byte(bkey(i)))
_, err = child.Get(hkey)
if err != storage.ErrKeyNotFound {
t.Fatalf("Expected storage.ErrKeyNotFound. Got %v", err)
}
}

stats := child.Stats()
if stats.Allocated != defaultTableSize {
t.Fatalf("Expected Stats.Allocated: %d. Got: %d", defaultTableSize, stats.Allocated)
}

if stats.Inuse != 0 {
t.Fatalf("Expected Stats.Inuse: 0. Got: %d", stats.Inuse)
}

if stats.Garbage != 0 {
t.Fatalf("Expected Stats.Garbage: 0. Got: %d", stats.Garbage)
}

if stats.Length != 0 {
t.Fatalf("Expected Stats.Length: 0. Got: %d", stats.Length)
}

if stats.NumTables != 1 {
t.Fatalf("Expected Stats.NumTables: 1. Got: %d", stats.NumTables)
}
}
2 changes: 2 additions & 0 deletions olric.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,11 @@ func (db *Olric) initializeAndLoadStorageEngines() error {

// Start the engines.
for _, engine := range db.storageEngines.engines {
engine.SetLogger(db.config.Logger)
if err := engine.Start(); err != nil {
return err
}
db.log.V(2).Printf("[INFO] Storage engine has been loaded: %s", engine.Name())
}
return nil
}
Expand Down
25 changes: 4 additions & 21 deletions olric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,16 @@ package olric
import (
"context"
"testing"
"time"
)

func TestOlric_StartedCallback(t *testing.T) {
func TestOlric_StartAndShutdown(t *testing.T) {
c := testSingleReplicaConfig()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
c.Started = func() {
cancel()
}

db, err := newDB(c)
if err != nil {
t.Fatalf("Expected nil. Got: %v", err)
}
defer func() {
err = db.Shutdown(context.Background())
if err != nil {
db.log.V(2).Printf("[ERROR] Failed to shutdown Olric: %v", err)
}
}()

select {
case <-time.After(31 * time.Second):
t.Fatalf("Failed to callback function in 30 seconds")
case <-ctx.Done():
if ctx.Err() != context.Canceled {
t.Fatalf("context returned an error: %v", ctx.Err())
}
err = db.Shutdown(context.Background())
if err != nil {
db.log.V(2).Printf("[ERROR] Failed to shutdown Olric: %v", err)
}
}
Loading

0 comments on commit 2875c0d

Please sign in to comment.