Created
June 19, 2015 16:15
-
-
Save awly/1fda7b11c38189618068 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"net" | |
) | |
type KVpair struct { | |
Key string | |
Value string | |
} | |
type Conversation struct { | |
Lines []KVpair | |
} | |
type Cass struct { | |
Address string | |
} | |
func (c *Cass) Send(conv Conversation) (string, error) { | |
conn, err := net.Dial("tcp", c.Address) | |
if err != nil { | |
return "", err | |
} | |
defer conn.Close() | |
for _, line := range conv.Lines { | |
if _, err := fmt.Fprintf(conn, "%s=%s\n", line.Key, line.Value); err != nil { | |
return "", err | |
} | |
} | |
if _, err := fmt.Fprintln(conn, "END"); err != nil { | |
return "", err | |
} | |
return bufio.NewReader(conn).ReadString('\n') | |
} |
This file contains hidden or 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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"net" | |
"os" | |
"testing" | |
) | |
func TestMain(m *testing.M) { | |
startCASSFixture() | |
result := m.Run() | |
os.Exit(result) | |
} | |
func startCASSFixture() { | |
listener, err := net.Listen("tcp", ":10101") | |
checkError(err) | |
fmt.Println("listening") | |
go func() { | |
for { | |
conn, err := listener.Accept() | |
if err != nil { | |
log.Println(err) | |
continue | |
} | |
go handleConn(conn) | |
} | |
}() | |
} | |
func handleConn(c net.Conn) { | |
defer c.Close() | |
reader := bufio.NewReader(c) | |
line, err := reader.ReadString('\n') | |
checkError(err) | |
log.Println("listener received:", line) | |
if _, err = c.Write([]byte("Hello WA\n")); err != nil { | |
log.Println(err) | |
return | |
} | |
} | |
func TestCASS(t *testing.T) { | |
c := Cass{"localhost:10101"} | |
reply, err := c.Send(Conversation{[]KVpair{{"ACTION", "NOTHING"}}}) | |
checkError(err) | |
fmt.Println(reply) | |
} | |
func checkError(err error) { | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Fatal Error: %s", err.Error()) | |
os.Exit(1) | |
} | |
} |
This file contains hidden or 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
listening | |
2015/06/19 09:14:45 listener received: ACTION=NOTHING | |
Hello WA | |
PASS | |
ok test 0.011s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment