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

Code before:
	"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 generateRandomTCPPort() int {
	rand.Seed(time.Now().UnixNano())
	min := 1024
	max := 64000
	return rand.Intn(max - min + 1)
}

func TestSafeset(t *testing.T) {
	key := base64.StdEncoding.EncodeToString([]byte("Pablo"))
	value := base64.StdEncoding.EncodeToString([]byte("Picasso"))
	tcpPort := generateRandomTCPPort()
	op := immudb.DefaultOptions().WithPort(tcpPort).WithDir("db_" + strconv.FormatInt(int64(tcpPort), 10))
	s := immudb.DefaultServer().WithOptions(op)
	go s.Start()
	time.Sleep(2 * time.Second)
	defer s.Stop()
	defer os.RemoveAll(op.Dir)

	ic, err := immuclient.NewImmuClient(immuclient.DefaultOptions().WithPort(tcpPort))
	if err != nil {
		t.Errorf("unable to instantiate client: %s", err)
		return
	}
	mux := runtime.NewServeMux()
	ssh := NewSafesetHandler(mux, ic)

	tt := []struct {
		test           string
		payload        string
		wantStatus     int
		wantFieldName  string
		wantFieldValue interface{}
	}{
		{ //Pablo = UGFibG8=
			//Picasso = UGljYXNzbw==
			"Send correct request",
			`{"kv": {"key": "` + key + `", "value": "` + value + `"	} }`,
			200,
			"verified",
			true,
		},

Review comment (bug):
When I launch test on my machine It returns following error:
```
 WARNING: LOG Compact FAILED with error: open db_57655/immudb/000002.sst: no such file or directory
While opening new table: 2
github.com/dgraph-io/badger/v2.(*levelsController).compactBuildTables.func2
	/home/falce/vchain/immudb/pkg/mod/github.com/dgraph-io/badger/v2@v2.0.0-20200408100755-2e708d968e94/levels.go:618
github.com/dgraph-io/badger/v2.(*levelsController).compactBuildTables.func3
	/home/falce/vchain/immudb/pkg/mod/github.com/dgraph-io/badger/v2@v2.0.0-20200408100755-2e708d968e94/levels.go:642
runtime.goexit
....
```
I belive that folder is deleted before badger stop:
try with something:
```
defer func() {
		s.Stop()
		os.RemoveAll(op.Dir)
	}()
```

Code after:
	"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 generateRandomTCPPort() int {
	rand.Seed(time.Now().UnixNano())
	min := 1024
	max := 64000
	return rand.Intn(max - min + 1)
}

func TestSafeset(t *testing.T) {
	key := base64.StdEncoding.EncodeToString([]byte("Pablo"))
	value := base64.StdEncoding.EncodeToString([]byte("Picasso"))
	tcpPort := generateRandomTCPPort()
	op := immudb.DefaultOptions().WithPort(tcpPort).WithDir("db_" + strconv.FormatInt(int64(tcpPort), 10))
	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)
	}()

	ic, err := immuclient.NewImmuClient(immuclient.DefaultOptions().WithPort(tcpPort))
	if err != nil {
		t.Errorf("unable to instantiate client: %s", err)
		return
	}
	mux := runtime.NewServeMux()
	ssh := NewSafesetHandler(mux, ic)

	tt := []struct {
		test           string
		payload        string
		wantStatus     int
		wantFieldName  string
		wantFieldValue interface{}
	}{
		{ //Pablo = UGFibG8=
			//Picasso = UGljYXNzbw==
			"Send correct request",
			`{"kv": {"key": "` + key + `", "value": "` + value + `"	} }`,
			http.StatusOK,