Added tests for all plugins

This commit is contained in:
Leonid Bugaev
2013-10-29 11:42:15 +01:00
parent 9dddab58d3
commit ce76453fa6
11 changed files with 323 additions and 16 deletions
+5 -6
View File
@@ -2,17 +2,16 @@ package gor
import (
"io"
"log"
"time"
)
func Start() {
func Start(stop chan int) {
for _, in := range Plugins.Inputs {
CopyMulty(in, Plugins.Outputs...)
}
for {
time.Sleep(time.Second)
select {
case <-stop:
return
}
}
@@ -23,7 +22,7 @@ func CopyMulty(src io.Reader, writers ...io.Writer) (err error) {
for {
nr, er := src.Read(buf)
if nr > 0 {
log.Println("Sending", src, ": ", string(buf[0:nr]))
Debug("Sending", src, ": ", string(buf[0:nr]))
for _, dst := range writers {
dst.Write(buf[0:nr])
+31
View File
@@ -0,0 +1,31 @@
package gor
import (
"io"
"sync"
"testing"
)
func TestEmitter(t *testing.T) {
wg := new(sync.WaitGroup)
quit := make(chan int)
input := NewTestInput()
output := NewTestOutput(func(data []byte) {
wg.Done()
})
Plugins.Inputs = []io.Reader{input}
Plugins.Outputs = []io.Writer{output}
go Start(quit)
for i := 0; i < 1000; i++ {
wg.Add(1)
input.EmitGET()
}
wg.Wait()
close(quit)
}
+1 -1
View File
@@ -54,7 +54,7 @@ func main() {
profileCPU(*cpuprofile)
}
gor.Start()
gor.Start(nil)
}
func profileCPU(cpuprofile string) {
+44
View File
@@ -0,0 +1,44 @@
package gor
import (
"io"
"net/http"
"sync"
"testing"
)
func TestRAWInput(t *testing.T) {
startHTTP := func(addr string, cb func(*http.Request)) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cb(r)
})
go http.ListenAndServe(addr, handler)
}
wg := new(sync.WaitGroup)
quit := make(chan int)
input := NewRAWInput("127.0.0.1:50004")
output := NewTestOutput(func(data []byte) {
wg.Done()
})
startHTTP("127.0.0.1:50004", func(req *http.Request) {
wg.Done()
})
Plugins.Inputs = []io.Reader{input}
Plugins.Outputs = []io.Writer{output}
go Start(quit)
for i := 0; i < 100; i++ {
wg.Add(1)
http.Get("http://127.0.0.1:50004")
}
wg.Wait()
close(quit)
}
+11 -9
View File
@@ -19,7 +19,7 @@ func NewTCPInput(address string) (i *TCPInput) {
i.data = make(chan []byte)
i.address = address
go i.listen(address)
i.listen(address)
return
}
@@ -38,16 +38,18 @@ func (i *TCPInput) listen(address string) {
log.Fatal("Can't start:", err)
}
for {
conn, err := listener.Accept()
go func() {
for {
conn, err := listener.Accept()
if err != nil {
log.Println("Error while Accept()", err)
continue
if err != nil {
log.Println("Error while Accept()", err)
continue
}
go i.handleConnection(conn)
}
go i.handleConnection(conn)
}
}()
}
func (i *TCPInput) handleConnection(conn net.Conn) {
+47
View File
@@ -0,0 +1,47 @@
package gor
import (
"io"
"log"
"net"
"sync"
"testing"
)
func TestTCPInput(t *testing.T) {
wg := new(sync.WaitGroup)
quit := make(chan int)
input := NewTCPInput("127.0.0.1:50001")
output := NewTestOutput(func(data []byte) {
wg.Done()
})
Plugins.Inputs = []io.Reader{input}
Plugins.Outputs = []io.Writer{output}
go Start(quit)
for i := 0; i < 100; i++ {
wg.Add(1)
sendTCP("127.0.0.1:50001", []byte("GET / HTTP/1.1\r\n\r\n"))
}
wg.Wait()
close(quit)
}
func sendTCP(addr string, data []byte) {
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
log.Fatal(err)
}
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
log.Fatal(err)
}
conn.Write(data)
}
+43
View File
@@ -0,0 +1,43 @@
package gor
import (
"io"
"net/http"
"sync"
"testing"
)
func TestHTTPOutput(t *testing.T) {
startHTTP := func(addr string, cb func(*http.Request)) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cb(r)
})
go http.ListenAndServe(addr, handler)
}
wg := new(sync.WaitGroup)
quit := make(chan int)
input := NewTestInput()
output := NewHTTPOutput("127.0.0.1:50003")
startHTTP("127.0.0.1:50003", func(req *http.Request) {
wg.Done()
})
Plugins.Inputs = []io.Reader{input}
Plugins.Outputs = []io.Writer{output}
go Start(quit)
for i := 0; i < 100; i++ {
wg.Add(2)
input.EmitGET()
input.EmitPOST()
}
wg.Wait()
close(quit)
}
+75
View File
@@ -0,0 +1,75 @@
package gor
import (
"io"
"log"
"net"
"sync"
"testing"
)
func TestTCPOutput(t *testing.T) {
wg := new(sync.WaitGroup)
quit := make(chan int)
input := NewTestInput()
output := NewTCPOutput(":50002")
startTCP(":50002", func(data []byte) {
wg.Done()
})
Plugins.Inputs = []io.Reader{input}
Plugins.Outputs = []io.Writer{output}
go Start(quit)
for i := 0; i < 100; i++ {
wg.Add(1)
input.EmitGET()
}
wg.Wait()
close(quit)
}
func startTCP(addr string, cb func([]byte)) {
listener, err := net.Listen("tcp", addr)
if err != nil {
log.Fatal("Can't start:", err)
}
go func() {
for {
conn, _ := listener.Accept()
var read = true
var response []byte
var buf []byte
buf = make([]byte, 4094)
for read {
n, err := conn.Read(buf)
switch err {
case io.EOF:
read = false
case nil:
response = append(response, buf[:n]...)
if n < 4096 {
read = false
}
default:
read = false
}
}
cb(response)
conn.Close()
}
}()
}
+11
View File
@@ -2,9 +2,12 @@ package gor
import (
"flag"
"log"
)
type AppSettings struct {
verbose bool
inputDummy MultiOption
outputDummy MultiOption
@@ -22,6 +25,8 @@ type AppSettings struct {
var Setttings AppSettings = AppSettings{}
func init() {
flag.BoolVar(&Setttings.verbose, "verbose", false, "")
flag.Var(&Setttings.inputDummy, "input-dummy", "")
flag.Var(&Setttings.outputDummy, "output-dummy", "")
@@ -35,3 +40,9 @@ func init() {
flag.Var(&Setttings.outputHTTP, "output-http", "")
}
func Debug(args ...interface{}) {
if Setttings.verbose {
log.Println(args...)
}
}
+31
View File
@@ -0,0 +1,31 @@
package gor
type TestInput struct {
data chan []byte
}
func NewTestInput() (i *TestInput) {
i = new(TestInput)
i.data = make(chan []byte)
return
}
func (i *TestInput) Read(data []byte) (int, error) {
buf := <-i.data
copy(data, buf)
return len(buf), nil
}
func (i *TestInput) EmitGET() {
i.data <- []byte("GET / HTTP/1.1\r\n\r\n")
}
func (i *TestInput) EmitPOST() {
i.data <- []byte("POST /pub/WWW/ HTTP/1.1\nHost: www.w3.org\r\n\r\na=1&b=2\r\n\r\n")
}
func (i *TestInput) String() string {
return "Test Input"
}
+24
View File
@@ -0,0 +1,24 @@
package gor
type writeCallback func(data []byte)
type TestOutput struct {
cb writeCallback
}
func NewTestOutput(cb writeCallback) (i *TestOutput) {
i = new(TestOutput)
i.cb = cb
return
}
func (i *TestOutput) Write(data []byte) (int, error) {
i.cb(data)
return len(data), nil
}
func (i *TestOutput) String() string {
return "Test Input"
}