Susumu Yata
null+****@clear*****
Thu Jul 27 17:22:34 JST 2017
Susumu Yata 2017-07-27 17:22:34 +0900 (Thu, 27 Jul 2017) New Revision: 5f90d4371e6e48700d2ee5121eee096f76b13b26 https://github.com/groonga/grnci/commit/5f90d4371e6e48700d2ee5121eee096f76b13b26 Message: Enable tests for libgrn.Client. Modified files: v2/libgrn/client_test.go Modified: v2/libgrn/client_test.go (+201 -97) =================================================================== --- v2/libgrn/client_test.go 2017-07-27 16:31:34 +0900 (b1197a8) +++ v2/libgrn/client_test.go 2017-07-27 17:22:34 +0900 (7704873) @@ -1,108 +1,212 @@ package libgrn import ( + "context" + "io" + "io/ioutil" + "log" + "os" + "os/exec" + "path/filepath" + "strings" "testing" + "time" "github.com/groonga/grnci/v2" ) -// func TestClientGQTP(t *testing.T) { -// type Pair struct { -// Command string -// Body string -// } -// pairs := []Pair{ -// // Pair{"no_such_command", ""}, -// Pair{"status", ""}, -// Pair{`table_create Tbl TABLE_PAT_KEY ShortText`, ""}, -// Pair{`column_create Tbl Col COLUMN_SCALAR Int32`, ""}, -// Pair{`load --table Tbl --values '[["_key"],["test"]]'`, ""}, -// Pair{`load --table Tbl --values '[["_key"],["test" invalid_format]]'`, ""}, -// Pair{"load --table Tbl", `[["_key"],["test"]]`}, -// Pair{"load --table Tbl", `[["_key"],["test" invalid_format]]`}, -// Pair{"select --table Tbl", ""}, -// Pair{"dump", ""}, -// } - -// client, err := DialClient("") -// if err != nil { -// t.Skipf("DialClient failed: %v", err) -// } -// defer client.Close() - -// for _, pair := range pairs { -// var body io.Reader -// if pair.Body != "" { -// body = strings.NewReader(pair.Body) -// } -// log.Printf("command = %s", pair.Command) -// resp, err := client.Exec(pair.Command, body) -// if err != nil { -// t.Fatalf("client.Exec failed: %v", err) -// } -// result, err := ioutil.ReadAll(resp) -// if err != nil { -// t.Fatalf("ioutil.ReadAll failed: %v", err) -// } -// log.Printf("start = %v, elapsed = %v", resp.Start(), resp.Elapsed()) -// log.Printf("result = %s", result) -// if err := resp.Err(); err != nil { -// log.Printf("err = %v", err) -// } -// if err := resp.Close(); err != nil { -// t.Fatalf("resp.Close failed: %v", err) -// } -// } -// } - -// func TestClientDB(t *testing.T) { -// type Pair struct { -// Command string -// Body string -// } -// pairs := []Pair{ -// // Pair{"no_such_command", ""}, -// Pair{"status", ""}, -// Pair{`table_create Tbl TABLE_PAT_KEY ShortText`, ""}, -// Pair{`column_create Tbl Col COLUMN_SCALAR Int32`, ""}, -// Pair{`load --table Tbl --values '[["_key"],["test"]]'`, ""}, -// Pair{`load --table Tbl --values '[["_key"],["test" invalid_format]]'`, ""}, -// Pair{"load --table Tbl", `[["_key"],["test"]]`}, -// Pair{"load --table Tbl", `[["_key"],["test" invalid_format]]`}, -// Pair{"select --table Tbl", ""}, -// Pair{"dump", ""}, -// } - -// client, err := OpenClient("/tmp/db/db") -// if err != nil { -// t.Skipf("OpenClient failed: %v", err) -// } -// defer client.Close() - -// for _, pair := range pairs { -// var body io.Reader -// if pair.Body != "" { -// body = strings.NewReader(pair.Body) -// } -// log.Printf("command = %s", pair.Command) -// resp, err := client.Exec(pair.Command, body) -// if err != nil { -// t.Fatalf("client.Exec failed: %v", err) -// } -// result, err := ioutil.ReadAll(resp) -// if err != nil { -// t.Fatalf("ioutil.ReadAll failed: %v", err) -// } -// log.Printf("start = %v, elapsed = %v", resp.Start(), resp.Elapsed()) -// log.Printf("result = %s", result) -// if err := resp.Err(); err != nil { -// log.Printf("err = %v", err) -// } -// if err := resp.Close(); err != nil { -// t.Fatalf("resp.Close failed: %v", err) -// } -// } -// } +type gqtpServer struct { + dir string + path string + cmd *exec.Cmd + cancel context.CancelFunc +} + +// newGQTPServer creates a new DB and starts a server. +func newGQTPServer(t *testing.T) *gqtpServer { + dir, err := ioutil.TempDir("", "grnci") + if err != nil { + log.Fatalf("ioutil.TempDir failed: %v", err) + } + + path := filepath.Join(dir, "db") + cmd := exec.Command("groonga", "-n", path) + stdin, _ := cmd.StdinPipe() + if err := cmd.Start(); err != nil { + os.RemoveAll(dir) + t.Skipf("cmd.Start failed: %v", err) + } + stdin.Close() + cmd.Wait() + + ctx, cancel := context.WithCancel(context.Background()) + cmd = exec.CommandContext(ctx, "groonga", "-s", "--protocol", "gqtp", path) + if err := cmd.Start(); err != nil { + os.RemoveAll(dir) + t.Skipf("cmd.Start failed: %v", err) + } + time.Sleep(time.Millisecond * 10) + + return &gqtpServer{ + dir: dir, + cmd: cmd, + cancel: cancel, + } +} + +// Close finishes the server and removes the DB. +func (s *gqtpServer) Close() { + s.cancel() + s.cmd.Wait() + os.RemoveAll(s.dir) +} + +func TestGQTPClient(t *testing.T) { + server := newGQTPServer(t) + defer server.Close() + + client, err := Dial("", nil) + if err != nil { + t.Skipf("Dial failed: %v", err) + } + defer client.Close() + + type Test struct { + Command string + Body string + Error bool + Success bool + } + tests := []Test{ + // Error: false, Success: true + Test{"status", "", false, true}, + Test{"table_create Tbl TABLE_PAT_KEY ShortText", "", false, true}, + Test{"column_create Tbl Col COLUMN_SCALAR Int32", "", false, true}, + Test{`load --table Tbl --values '[["_key"],["test"]]'`, "", false, true}, + Test{"load --table Tbl", `[["_key"],["test"]]`, false, true}, + Test{"select --table Tbl", "", false, true}, + Test{"dump", "", false, true}, + // Error: true, Success: * + Test{"no_such_command", "", true, false}, + Test{"status", "body is not acceptable", true, false}, + // Error: false, Success: false + Test{"table_create Tbl2", "", false, false}, + Test{`load --table Tbl --values '[["_key"],["test" invalid_format]]'`, "", false, false}, + Test{"load --table Tbl", `[["_key"],["test" invalid_format]]`, false, false}, + } + + for _, test := range tests { + var body io.Reader + if test.Body != "" { + body = strings.NewReader(test.Body) + } + resp, err := client.Exec(test.Command, body) + if test.Error { + if err != nil { + continue + } + t.Fatalf("client.Exec wrongly succeeded: cmd = %s", test.Command) + } else { + if err != nil { + t.Fatalf("conn.Exec failed: cmd = %s, err = %v", test.Command, err) + } + } + respBody, err := ioutil.ReadAll(resp) + if err != nil { + t.Fatalf("ioutil.ReadAll failed: cmd = %s, err = %v", test.Command, err) + } + if test.Success { + if err := resp.Err(); err != nil { + t.Fatalf("client.Exec failed: cmd = %s, err = %v", test.Command, err) + } + if len(respBody) == 0 { + t.Fatalf("ioutil.ReadAll failed: cmd = %s, len(respBody) = 0", test.Command) + } + } else { + if err := resp.Err(); err == nil { + t.Fatalf("client.Exec wrongly succeeded: cmd = %s", test.Command) + } + } + if err := resp.Close(); err != nil { + t.Fatalf("resp.Close failed: %v", err) + } + } +} + +func TestDBClient(t *testing.T) { + dir, err := ioutil.TempDir("", "grnci") + if err != nil { + log.Fatalf("ioutil.TempDir failed: %v", err) + } + defer os.RemoveAll(dir) + + client, err := Create(filepath.Join(dir, "db"), nil) + if err != nil { + t.Skipf("Dial failed: %v", err) + } + defer client.Close() + + type Test struct { + Command string + Body string + Error bool + Success bool + } + tests := []Test{ + // Error: false, Success: true + Test{"status", "", false, true}, + Test{"table_create Tbl TABLE_PAT_KEY ShortText", "", false, true}, + Test{"column_create Tbl Col COLUMN_SCALAR Int32", "", false, true}, + Test{`load --table Tbl --values '[["_key"],["test"]]'`, "", false, true}, + Test{"load --table Tbl", `[["_key"],["test"]]`, false, true}, + Test{"select --table Tbl", "", false, true}, + Test{"dump", "", false, true}, + // Error: true, Success: * + Test{"no_such_command", "", true, false}, + Test{"status", "body is not acceptable", true, false}, + // Error: false, Success: false + Test{"table_create Tbl2", "", false, false}, + Test{`load --table Tbl --values '[["_key"],["test" invalid_format]]'`, "", false, false}, + Test{"load --table Tbl", `[["_key"],["test" invalid_format]]`, false, false}, + } + + for _, test := range tests { + var body io.Reader + if test.Body != "" { + body = strings.NewReader(test.Body) + } + resp, err := client.Exec(test.Command, body) + if test.Error { + if err != nil { + continue + } + t.Fatalf("client.Exec wrongly succeeded: cmd = %s", test.Command) + } else { + if err != nil { + t.Fatalf("conn.Exec failed: cmd = %s, err = %v", test.Command, err) + } + } + respBody, err := ioutil.ReadAll(resp) + if err != nil { + t.Fatalf("ioutil.ReadAll failed: cmd = %s, err = %v", test.Command, err) + } + if test.Success { + if err := resp.Err(); err != nil { + t.Fatalf("client.Exec failed: cmd = %s, err = %v", test.Command, err) + } + if len(respBody) == 0 { + t.Fatalf("ioutil.ReadAll failed: cmd = %s, len(respBody) = 0", test.Command) + } + } else { + if err := resp.Err(); err == nil { + t.Fatalf("client.Exec wrongly succeeded: cmd = %s", test.Command) + } + } + if err := resp.Close(); err != nil { + t.Fatalf("resp.Close failed: %v", err) + } + } +} func TestClientHandler(t *testing.T) { var i interface{} = &Client{} -------------- next part -------------- HTML����������������������������... Descargar