-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server to only manage tcp/unix socket
- Loading branch information
yowcow
committed
May 28, 2018
1 parent
89b759e
commit a3e1797
Showing
3 changed files
with
59 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,155 +1,71 @@ | ||
package server | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/yowcow/goromdb/handler/simplehandler" | ||
"github.com/yowcow/goromdb/protocol" | ||
"github.com/yowcow/goromdb/storage" | ||
"github.com/yowcow/goromdb/testutil" | ||
) | ||
|
||
type TestKeywords map[string][][]byte | ||
|
||
type TestProtocol struct { | ||
keywords TestKeywords | ||
} | ||
|
||
func createTestProtocol() protocol.Protocol { | ||
keywords := TestKeywords{ | ||
"hoge": {[]byte("foo"), []byte("bar")}, | ||
} | ||
return &TestProtocol{keywords} | ||
} | ||
|
||
func (p TestProtocol) Parse(line []byte) ([][]byte, error) { | ||
if words, ok := p.keywords[string(line)]; ok { | ||
return words, nil | ||
} | ||
return [][]byte{}, fmt.Errorf("invalid command") | ||
} | ||
|
||
func (p TestProtocol) Reply(w io.Writer, key, value []byte) { | ||
w.Write(key) | ||
w.Write([]byte(" ")) | ||
w.Write(value) | ||
w.Write([]byte("\r\n")) | ||
} | ||
|
||
func (p TestProtocol) Finish(w io.Writer) { | ||
w.Write([]byte("BYE\r\n")) | ||
} | ||
|
||
type TestData map[string]string | ||
|
||
type TestStorage struct { | ||
data TestData | ||
logger *log.Logger | ||
} | ||
|
||
func createTestStorage(logger *log.Logger) storage.Storage { | ||
data := TestData{ | ||
"foo": "foo!", | ||
"bar": "bar!!", | ||
} | ||
return &TestStorage{data, logger} | ||
} | ||
|
||
func (s TestStorage) Load(file string) error { | ||
return nil | ||
} | ||
|
||
func (s TestStorage) Get(key []byte) ([]byte, error) { | ||
if v, ok := s.data[string(key)]; ok { | ||
return []byte(v), nil | ||
} | ||
return nil, storage.KeyNotFoundError(key) | ||
} | ||
|
||
func TestHandleConn(t *testing.T) { | ||
dir := testutil.CreateTmpDir() | ||
defer os.RemoveAll(dir) | ||
|
||
logbuf := new(bytes.Buffer) | ||
logger := log.New(logbuf, "", 0) | ||
p := createTestProtocol() | ||
stg := createTestStorage(logger) | ||
h := simplehandler.New(stg, logger) | ||
|
||
sock := filepath.Join(dir, "test.sock") | ||
svr := New("unix", sock, p, h, logger) | ||
|
||
done := make(chan bool) | ||
ln, err := net.Listen("unix", sock) | ||
if err != nil { | ||
panic(err) | ||
} | ||
go func() { | ||
defer close(done) | ||
for { | ||
conn, err := ln.Accept() | ||
if err != nil { | ||
break | ||
} | ||
svr.HandleConn(conn) | ||
} | ||
}() | ||
svr := New("unix", sock, logger) | ||
|
||
type Case struct { | ||
input string | ||
expected []string | ||
subtest string | ||
subtest string | ||
input []byte | ||
expectedLine []byte | ||
} | ||
cases := []Case{ | ||
{ | ||
"hoge\r\n", | ||
[]string{ | ||
"foo foo!", | ||
"bar bar!!", | ||
"BYE", | ||
}, | ||
"hoge returns 3 lines of message", | ||
}, | ||
{ | ||
"fuga\r\n", | ||
[]string{ | ||
"BYE", | ||
}, | ||
"fuga returns 1 line of message", | ||
"a line that end with \\r\\n", | ||
[]byte("hello world\r\n"), | ||
[]byte("hello world"), | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.subtest, func(t *testing.T) { | ||
conn, err := net.Dial("unix", sock) | ||
if err != nil { | ||
panic(err) | ||
done := make(chan bool) | ||
|
||
ln, err := net.Listen("unix", sock) | ||
if err != nil { | ||
panic(err) | ||
} | ||
go func() { | ||
defer close(done) | ||
for { | ||
conn, err := ln.Accept() | ||
if err != nil { | ||
return | ||
} | ||
svr.HandleConn(conn, OnReadCallbackFunc(func(conn net.Conn, line []byte, logger *log.Logger) { | ||
assert.Equal(t, c.expectedLine, line) | ||
})) | ||
} | ||
defer conn.Close() | ||
}() | ||
|
||
r := bufio.NewReader(conn) | ||
_, err = conn.Write([]byte(c.input)) | ||
conn, err := net.Dial("unix", sock) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
assert.Nil(t, err) | ||
_, err = conn.Write(c.input) | ||
|
||
for _, row := range c.expected { | ||
actual, _, err := r.ReadLine() | ||
conn.Close() | ||
ln.Close() | ||
<-done | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, row, string(actual)) | ||
} | ||
}) | ||
assert.Nil(t, err) | ||
} | ||
|
||
ln.Close() | ||
<-done | ||
} |