Repository: codenotary/immudb
File: pkg/gw/safezadd_handler_test.go (Go)

Code before:
	"net/http/httptest"
	"os"
	"strconv"
	"strings"
	"testing"
	"time"

	immuclient "github.com/codenotary/immudb/pkg/client"
	immudb "github.com/codenotary/immudb/pkg/server"
	"github.com/grpc-ecosystem/grpc-gateway/runtime"
)

func TestSafeZAdd(t *testing.T) {
	setName := base64.StdEncoding.EncodeToString([]byte("Soprano"))
	uknownKey := base64.StdEncoding.EncodeToString([]byte("Marias Callas"))
	tcpPort := generateRandomTCPPort()
	//MetricsServer must not be started as during tests because prometheus lib panis with: duplicate metrics collector registration attempted
	op := immudb.DefaultOptions().WithPort(tcpPort).WithDir("db_" + strconv.FormatInt(int64(tcpPort), 10)).WithMetricsServer(false)
	s := immudb.DefaultServer().WithOptions(op)
	go s.Start()
	time.Sleep(2 * time.Second)
	defer func() {
		s.Stop()
		time.Sleep(2 * time.Second) //without the delay the db dir is deleted before all the data has been flushed to disk and results in crash.
		os.RemoveAll(op.Dir)
	}()

	refkey, err := insertSampleSet(tcpPort)
	if err != nil {
		t.Errorf("%s", err)
	}
	ic, err := immuclient.NewImmuClient(immuclient.DefaultOptions().WithPort(tcpPort))

	if err != nil {
		t.Errorf("unable to instantiate client: %s", err)
		return
	}
	mux := runtime.NewServeMux()
	ssh := NewSafeZAddHandler(mux, ic)

	tt := []struct {
		test           string
		payload        string
		wantStatus     int
		wantFieldName  string
		wantFieldValue interface{}
	}{
		{
			"Send correct request",
			`{
				"zopts": {

Review comment (performance):
In order to avoid to slower down tests execution with sleeps we could add a method on immudb like store.isClosed or similar and then loop a that bool. What's your opinion?

Code after:
	"net/http/httptest"
	"os"
	"strconv"
	"strings"
	"testing"
	"time"

	immuclient "github.com/codenotary/immudb/pkg/client"
	immudb "github.com/codenotary/immudb/pkg/server"
	"github.com/grpc-ecosystem/grpc-gateway/runtime"
)

func TestSafeZAdd(t *testing.T) {
	setName := base64.StdEncoding.EncodeToString([]byte("Soprano"))
	uknownKey := base64.StdEncoding.EncodeToString([]byte("Marias Callas"))
	tcpPort := generateRandomTCPPort()
	//MetricsServer must not be started as during tests because prometheus lib panics with: duplicate metrics collector registration attempted
	op := immudb.DefaultOptions().
		WithPort(tcpPort).WithDir("db_" + strconv.FormatInt(int64(tcpPort), 10)).
		WithMetricsServer(false).WithCorruptionCheck(false)
	s := immudb.DefaultServer().WithOptions(op)
	go s.Start()
	time.Sleep(2 * time.Second)
	defer func() {
		s.Stop()
		time.Sleep(2 * time.Second) //without the delay the db dir is deleted before all the data has been flushed to disk and results in crash.
		os.RemoveAll(op.Dir)
	}()

	refkey, err := insertSampleSet(tcpPort)
	if err != nil {
		t.Errorf("%s", err)
	}
	ic, err := immuclient.NewImmuClient(immuclient.DefaultOptions().WithPort(tcpPort))

	if err != nil {
		t.Errorf("unable to instantiate client: %s", err)
		return
	}
	mux := runtime.NewServeMux()
	ssh := NewSafeZAddHandler(mux, ic)

	tt := []struct {
		test           string
		payload        string
		wantStatus     int
		wantFieldName  string
		wantFieldValue interface{}
	}{
		{
			"Send correct request",