Unverified Commit ca367e49 authored by Marty Schoch's avatar Marty Schoch Committed by GitHub
Browse files

Update match registry and matchmaker tests to not rely on time.Sleep. (#720)

parent f65cd46a
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -38,8 +38,9 @@ func loggerForTest(t *testing.T) *zap.Logger {
// createTestMatchRegistry creates a LocalMatchRegistry minimally configured for testing purposes
// In addition to the MatchRegistry, a RuntimeMatchCreateFunction paired to work with it is returned.
// This RuntimeMatchCreateFunction may be needed for later operations (such as CreateMatch)
func createTestMatchRegistry(t *testing.T, logger *zap.Logger) (MatchRegistry, RuntimeMatchCreateFunction, error) {
func createTestMatchRegistry(t *testing.T, logger *zap.Logger) (*LocalMatchRegistry, RuntimeMatchCreateFunction, error) {
	cfg := NewConfig(logger)
	cfg.GetMatch().LabelUpdateIntervalMs = int(time.Hour / time.Millisecond)
	messageRouter := &testMessageRouter{}
	matchRegistry := NewLocalMatchRegistry(logger, logger, cfg, &testSessionRegistry{}, &testTracker{},
		messageRouter, &testMetrics{}, "node")
@@ -61,7 +62,7 @@ func createTestMatchRegistry(t *testing.T, logger *zap.Logger) (MatchRegistry, R
			return rmc, nil
		})

	return matchRegistry, mp.CreateMatch, nil
	return matchRegistry.(*LocalMatchRegistry), mp.CreateMatch, nil
}

type testMatchState struct {
+8 −9
Original line number Diff line number Diff line
@@ -21,12 +21,11 @@ import (
	"fmt"
	"strings"
	"testing"
	"time"

	"github.com/blugelabs/bluge"
	"github.com/gofrs/uuid"
	"google.golang.org/protobuf/types/known/wrapperspb"

	"github.com/heroiclabs/nakama-common/runtime"
	"google.golang.org/protobuf/types/known/wrapperspb"
)

func TestEncode(t *testing.T) {
@@ -90,7 +89,7 @@ func TestMatchRegistryAuthoritativeMatchAndListMatches(t *testing.T) {
		t.Fatal(err)
	}

	time.Sleep(5 * time.Second)
	matchRegistry.processLabelUpdates(bluge.NewBatch())

	matches, err := matchRegistry.ListMatches(context.Background(), 2, wrapperspb.Bool(true),
		wrapperspb.String("label"), wrapperspb.Int32(0), wrapperspb.Int32(5), nil)
@@ -125,7 +124,7 @@ func TestMatchRegistryAuthoritativeMatchAndListMatchesWithTokenizableLabel(t *te
		t.Fatal(err)
	}

	time.Sleep(5 * time.Second)
	matchRegistry.processLabelUpdates(bluge.NewBatch())

	matches, err := matchRegistry.ListMatches(context.Background(), 2, wrapperspb.Bool(true),
		wrapperspb.String("label-part2"), wrapperspb.Int32(0), wrapperspb.Int32(5), nil)
@@ -158,7 +157,7 @@ func TestMatchRegistryAuthoritativeMatchAndListMatchesWithQuerying(t *testing.T)
		t.Fatal(err)
	}

	time.Sleep(5 * time.Second)
	matchRegistry.processLabelUpdates(bluge.NewBatch())

	matches, err := matchRegistry.ListMatches(context.Background(), 2, wrapperspb.Bool(true),
		wrapperspb.String("label"), wrapperspb.Int32(0), wrapperspb.Int32(5),
@@ -192,7 +191,7 @@ func TestMatchRegistryAuthoritativeMatchAndListAllMatchesWithQueryStar(t *testin
		t.Fatal(err)
	}

	time.Sleep(5 * time.Second)
	matchRegistry.processLabelUpdates(bluge.NewBatch())

	matches, err := matchRegistry.ListMatches(context.Background(), 2, wrapperspb.Bool(true),
		wrapperspb.String("label"), wrapperspb.Int32(0), wrapperspb.Int32(5),
@@ -230,7 +229,7 @@ func TestMatchRegistryAuthoritativeMatchAndListMatchesWithQueryingArrays(t *test
		t.Fatal(err)
	}

	time.Sleep(5 * time.Second)
	matchRegistry.processLabelUpdates(bluge.NewBatch())

	matches, err := matchRegistry.ListMatches(context.Background(), 2, wrapperspb.Bool(true),
		wrapperspb.String("label"), wrapperspb.Int32(0), wrapperspb.Int32(5),
@@ -280,7 +279,7 @@ func TestMatchRegistryAuthoritativeMatchAndListMatchesWithQueryingAndBoost(t *te
		}
	}

	time.Sleep(5 * time.Second)
	matchRegistry.processLabelUpdates(bluge.NewBatch())

	tests := []struct {
		name         string
+16 −15
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import (
	"testing"
	"time"

	"github.com/blugelabs/bluge"
	"github.com/gofrs/uuid"
	"github.com/heroiclabs/nakama-common/rtapi"
	"go.uber.org/zap"
@@ -143,7 +144,7 @@ func TestMatchmakerAddWithBasicMatch(t *testing.T) {
		t.Fatal("expected non-empty ticket2")
	}

	time.Sleep(5 * time.Second)
	matchMaker.process(bluge.NewBatch())

	// assert session 1 sees the match, and has expected details
	if mm, ok := matchesSeen[sessionID.String()]; ok {
@@ -261,7 +262,7 @@ func TestMatchmakerAddWithMatchOnStar(t *testing.T) {
		t.Fatal("expected non-empty ticket2")
	}

	time.Sleep(5 * time.Second)
	matchMaker.process(bluge.NewBatch())

	// assert session 1 sees the match, and has expected details
	if mm, ok := matchesSeen[sessionID.String()]; ok {
@@ -379,7 +380,7 @@ func TestMatchmakerAddWithMatchOnRange(t *testing.T) {
		t.Fatal("expected non-empty ticket2")
	}

	time.Sleep(5 * time.Second)
	matchMaker.process(bluge.NewBatch())

	// assert session 1 sees the match, and has expected details
	if mm, ok := matchesSeen[sessionID.String()]; ok {
@@ -503,7 +504,7 @@ func TestMatchmakerAddWithMatchOnRangeAndValue(t *testing.T) {
		t.Fatal("expected non-empty ticket2")
	}

	time.Sleep(5 * time.Second)
	matchMaker.process(bluge.NewBatch())

	// assert session 1 sees the match, and has expected details
	if mm, ok := matchesSeen[sessionID.String()]; ok {
@@ -606,7 +607,7 @@ func TestMatchmakerAddRemoveNotMatch(t *testing.T) {
		t.Fatalf("error matchmaker remove: %v", err)
	}

	time.Sleep(5 * time.Second)
	matchMaker.process(bluge.NewBatch())

	if len(matchesSeen) > 0 {
		t.Fatalf("expected 0 matches, got %d", len(matchesSeen))
@@ -674,7 +675,7 @@ func TestMatchmakerAddButNotMatch(t *testing.T) {
		t.Fatal("expected non-empty ticket2")
	}

	time.Sleep(5 * time.Second)
	matchMaker.process(bluge.NewBatch())

	if len(matchesSeen) > 0 {
		t.Fatalf("expected 0 matches, got %d", len(matchesSeen))
@@ -751,7 +752,7 @@ func TestMatchmakerAddButNotMatchOnRange(t *testing.T) {
		t.Fatal("expected non-empty ticket2")
	}

	time.Sleep(5 * time.Second)
	matchMaker.process(bluge.NewBatch())

	if len(matchesSeen) > 0 {
		t.Fatalf("expected 0 matches, got %d", len(matchesSeen))
@@ -830,7 +831,7 @@ func TestMatchmakerAddButNotMatchOnRangeAndValue(t *testing.T) {
		t.Fatal("expected non-empty ticket2")
	}

	time.Sleep(5 * time.Second)
	matchMaker.process(bluge.NewBatch())

	if len(matchesSeen) > 0 {
		t.Fatalf("expected 0 matches, got %d", len(matchesSeen))
@@ -926,7 +927,7 @@ func TestMatchmakerAddMultipleAndSomeMatch(t *testing.T) {
		t.Fatal("expected non-empty ticket3")
	}

	time.Sleep(5 * time.Second)
	matchMaker.process(bluge.NewBatch())

	// assert that 2 are notified of a match
	if len(matchesSeen) != 2 {
@@ -1035,7 +1036,7 @@ func TestMatchmakerAddMultipleAndSomeMatchWithBoost(t *testing.T) {
		t.Fatal("expected non-empty ticket3")
	}

	time.Sleep(5 * time.Second)
	matchMaker.process(bluge.NewBatch())

	if len(matchesSeen) != 2 {
		t.Fatalf("expected 2 matches, got %d", len(matchesSeen))
@@ -1148,7 +1149,7 @@ func TestMatchmakerAddMultipleAndSomeMatchOptionalTextAlteringScore(t *testing.T
		t.Fatal("expected non-empty ticket3")
	}

	time.Sleep(5 * time.Second)
	matchMaker.process(bluge.NewBatch())

	// assert that 2 are notified of a match
	if len(matchesSeen) != 2 {
@@ -1221,7 +1222,7 @@ func TestMatchmakerAddAndMatchAuthoritative(t *testing.T) {
		t.Fatal("expected non-empty ticket2")
	}

	time.Sleep(5 * time.Second)
	matchMaker.process(bluge.NewBatch())

	// assert session 1 sees the match, and has expected details
	if mm, ok := matchesSeen[sessionID.String()]; ok {
@@ -1289,9 +1290,9 @@ func TestMatchmakerAddAndMatchAuthoritative(t *testing.T) {
// the returned cleanup function should be executed after all test operations are complete
// to ensure proper resource management
func createTestMatchmaker(t *testing.T, logger *zap.Logger,
	messageCallback func(presences []*PresenceID, envelope *rtapi.Envelope)) (Matchmaker, func() error, error) {
	messageCallback func(presences []*PresenceID, envelope *rtapi.Envelope)) (*LocalMatchmaker, func() error, error) {
	cfg := NewConfig(logger)
	cfg.Matchmaker.IntervalSec = 1
	cfg.Matchmaker.IntervalSec = int(time.Hour / time.Second)
	// configure a path runtime can use (it will mkdir this, so it must be writable)
	var err error
	cfg.Runtime.Path, err = ioutil.TempDir("", "nakama-matchmaker-test")
@@ -1352,7 +1353,7 @@ func createTestMatchmaker(t *testing.T, logger *zap.Logger,

	matchMaker := NewLocalMatchmaker(logger, logger, cfg, messageRouter, runtime)

	return matchMaker, func() error {
	return matchMaker.(*LocalMatchmaker), func() error {
		matchMaker.Stop()
		matchRegistry.Stop(0)
		return os.RemoveAll(cfg.Runtime.Path)