Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f7b3522
feat: add test containers dependency
liooooo29 Aug 4, 2024
be67aa6
feat: rename close -> cliClientClose to avoid conflicts with built-in…
liooooo29 Aug 4, 2024
393d1cf
feat: add grpc service test container
liooooo29 Aug 4, 2024
ef04a09
feat: use docker multi-stage build to eliminate dependencies on exter…
liooooo29 Aug 4, 2024
f228f49
feat: add TestMain to exec flyDB client & scan stdout & start test co…
liooooo29 Aug 4, 2024
0128c14
feat: standardize error handling and error print
liooooo29 Aug 4, 2024
2e42e82
feat: add string test
liooooo29 Aug 4, 2024
d257ce1
fix: grpc hash service LRem method to handle request value type corre…
liooooo29 Aug 4, 2024
6a3a15d
feat: add list test
liooooo29 Aug 4, 2024
dc70829
fix: HMove method in the hash structure not working
liooooo29 Aug 4, 2024
5282120
fix: Hash client hashGetData function handle value is empty
liooooo29 Aug 4, 2024
7646e5e
fix: Hash client hashHType function, use flags to args
liooooo29 Aug 4, 2024
50c63ff
feat: add hash test
liooooo29 Aug 4, 2024
31221db
feat: add set test
liooooo29 Aug 4, 2024
d0872e1
feat: add zset test
liooooo29 Aug 4, 2024
7d6ba03
ci: update go.yml to add cli integration test
liooooo29 Aug 4, 2024
7ed81d3
fix: update hash unit test
liooooo29 Aug 4, 2024
ca9a726
fix: go test not found app_test.go
liooooo29 Aug 4, 2024
12ad39d
fix: zset test key typo error
liooooo29 Aug 4, 2024
3a47ba8
fix: cmd server address
liooooo29 Aug 4, 2024
9818c85
ci: change IntegrationTest to a single job
liooooo29 Aug 4, 2024
ab473ae
feat: change test case wait time
liooooo29 Aug 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ jobs:
done
exit 1
fi



- name: Lint
uses: golangci/golangci-lint-action@v3.6.0
Expand All @@ -52,4 +52,28 @@ jobs:
run: go build -v ./...

- name: Test
run: go test -v ./...
run: go list ./... | grep -v "./cmd/client" | xargs go test -v

IntegrationTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.18'

- name: Hash Structure Integration test
run: go test -v -a -tags hash_integration ./cmd/client/...

- name: List Structure Integration test
run: go test -v -a -tags list_integration ./cmd/client/...

- name: Set Structure Integration test
run: go test -v -a -tags set_integration ./cmd/client/...

- name: String Structure Integration test
run: go test -v -a -tags string_integration ./cmd/client/...

- name: ZSet Structure Integration test
run: go test -v -a -tags zset_integration ./cmd/client/...
2 changes: 1 addition & 1 deletion cmd/client/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ func init() {
\/__/ \/__/ \/__/ \/__/
`}, "\r\n"))
})
App.OnClosing(close)
App.OnClosing(cliClientClose)
register(App)
}
219 changes: 219 additions & 0 deletions cmd/client/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package client

import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
"testing"
"time"

"github.com/ByteStorage/FlyDB/db/grpc/service"
)

var (
stdoutPipe io.ReadCloser
stdoutScanner *bufio.Scanner
stderr = new(bytes.Buffer)
stdin io.WriteCloser
stdoutStringCh = make(chan string)
currentIndex = 0
)

func checkStdoutTextIsEmptyOrNot() {
for {
select {
case stdoutText := <-stdoutStringCh:
if stdoutText != "" {
panic(fmt.Sprintf("stdout text is not empty: %s", stdoutText))
}
case <-time.After(1 * time.Second):
return
}
}
}

func closeFlyDBClient(cmd *exec.Cmd) func() {
return func() {
var err error

checkStdoutTextIsEmptyOrNot()

close(stdoutStringCh)

if err = stdin.Close(); err != nil {
fmt.Println("close std in err: ", stderr)
panic(err)
}

if err = cmd.Wait(); err != nil {
fmt.Println("cmd wait err: ", stderr)
panic(err)
}
}
}

func setupStdinPipe(cmd *exec.Cmd) {
var (
err error
)

if stdin, err = cmd.StdinPipe(); err != nil {
panic(err)
}
}

func setupStdout(cmd *exec.Cmd) {
var (
err error
)

if stdoutPipe, err = cmd.StdoutPipe(); err != nil {
panic(err)
}

stdoutScanner = bufio.NewScanner(stdoutPipe)
}

func cmdStart(cmd *exec.Cmd) {
var (
err error
)

if err = cmd.Start(); err != nil {
panic(err)
}
}

func checkFlyDBOutput(currentText string) bool {
ignoreMessages := []string{
" ___ ___ ___ ___ ___ ",
" /\\ \\ /\\__\\ /\\__\\ /\\ \\ /\\ \\ ",
" /::\\ \\ /:/ / /:/ / ___ /::\\ \\ /::\\ \\ ",
" /:/\\ \\ \\ /:/ / /:/ / /\\__\\ /:/\\ \\ \\ /:/\\ \\ \\ ",
" /::\\~\\ \\ \\ /:/ / \\:\\ \\/ / / /:/ /\\ \\ \\ /::\\ \\ \\ \\ ",
" /:/\\:\\ \\ \\__\\ /:/ / __\\:\\~/ / / /:/_/ \\ \\ \\ /:/\\:\\ \\ \\ \\ ",
" \\/__\\:\\ \\/__/ \\:\\ \\ /\\ \\:::/ / \\:\\ \\ | | | \\:\\ \\:\\/ | | ",
" \\:\\__\\ \\:\\ \\ \\:\\~~/:/ / \\:\\~\\/ / / \\:\\ \\::/ / ",
" \\/__/ \\:\\ \\ \\:\\/:/ / \\:\\/ / / \\:\\/:/ / ",
" \\ \\__\\ \\::/ / \\::/ / \\::/ / ",
" \\/__/ \\/__/ \\/__/ \\/__/ ",
}

if currentIndex < len(ignoreMessages) && currentText == ignoreMessages[currentIndex] {
currentIndex++
return true
}

return false
}

func startFlyDBClient() func() {
cmd := exec.Command("go", "run", "./cli/flydb-client.go", Addr)

cmd.Stderr = stderr

setupStdinPipe(cmd)

setupStdout(cmd)

go func() {

for stdoutScanner.Scan() {
currentText := stdoutScanner.Text()

if currentText == "" {
continue
}
if checkFlyDBOutput(currentText) {
continue
}

stdoutStringCh <- stdoutScanner.Text()
}
}()

cmdStart(cmd)

return closeFlyDBClient(cmd)
}

type cmdMeta struct {
cmd string
expectResult []string
testcaseName string
}

func (c *cmdMeta) lofFileAndLine(t *testing.T, funcName string) {
found := false
for skip := 0; !found; skip++ {
pc, file, line, ok := runtime.Caller(skip)
if !ok {
break
}
details := runtime.FuncForPC(pc)
if details != nil && strings.Contains(details.Name(), funcName) {
t.Logf("TestCase : %s in %s:%d", funcName, file, line)
found = true
}
}
if !found {
t.Logf("File: %s, Line: %d", "unknown", 0)
}
}

func (c *cmdMeta) cmdTest(t *testing.T) {
var (
err error
stdoutString string
)

if _, err = stdin.Write([]byte(c.cmd + "\n")); err != nil {
t.Fatal(err)
}

for _, expectResult := range c.expectResult {
select {
case stdoutString = <-stdoutStringCh:
case <-time.After(10 * time.Second):
c.lofFileAndLine(t, c.testcaseName)
t.Fatal("not get stdout")
}

if strings.Contains(stdoutString, expectResult) == false {
c.lofFileAndLine(t, c.testcaseName)
t.Fatalf("expect: %s\n got: %s\n", expectResult, stdoutString)
}
}

}

func TestMain(m *testing.M) {
var (
ctx = context.Background()
err error
serviceContainer = &service.Container{}
closeServiceContainerFunc func()
closeFlyDBClientFunc func()
)

if closeServiceContainerFunc, err = serviceContainer.StartServiceContainer(ctx, flyDBServerPort); err != nil {
panic(err)
}

Addr = serviceContainer.URI

closeFlyDBClientFunc = startFlyDBClient()

code := m.Run()

closeFlyDBClientFunc()
closeServiceContainerFunc()

os.Exit(code)
}
6 changes: 2 additions & 4 deletions cmd/client/client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package client

import (
"fmt"

"github.com/ByteStorage/FlyDB/db/grpc/client"
)

Expand All @@ -18,13 +16,13 @@ func newClient() *client.Client {
}

if cliClient, err = client.NewClient(Addr); err != nil {
fmt.Println("new client error: ", err)
panic(err)
}

return cliClient
}

func close() error {
func cliClientClose() error {
if cliClient == nil {
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/client/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ const (
ZSetDefaultMinHelp = "the min score of the zset"
ZSetDefaultMaxHelp = "the max score of the zset"
)

const (
flyDBServerPort = "8999/tcp"
)
18 changes: 15 additions & 3 deletions cmd/client/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func hashHGetData(c *grumble.Context) error {
fmt.Println("get data error: ", err)
return err
}

if value == nil {
fmt.Println("Field or Key does not exist in the hash")
return nil
}

fmt.Println(value)
return nil
}
Expand Down Expand Up @@ -209,8 +215,8 @@ func hashHSetNXKey(c *grumble.Context) error {
}

func hashHType(c *grumble.Context) error {
key := c.Flags.String("key")
field := c.Flags.String("field")
key := c.Args.String("key")
field := c.Args.String("field")

if key == "" {
return errors.New("key is empty")
Expand All @@ -221,7 +227,13 @@ func hashHType(c *grumble.Context) error {
}
hashType, err := newClient().HType(key, field)
if err != nil {
return errors.Wrap(err, "HType error")
fmt.Println("HType error: ", err)
return err
}

if hashType == "" {
fmt.Println("Field or Key does not exist in the hash")
return nil
}

fmt.Println("Type of field", field, "in hash", key, "is", hashType)
Expand Down
Loading