Skip to content

Commit

Permalink
improved the test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
zond committed Feb 18, 2014
1 parent b68ed3a commit 1f40693
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
15 changes: 11 additions & 4 deletions client/client_test.go → client/examples/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package client

import (
"fmt"

"github.com/zond/god/client"
"github.com/zond/god/common"
"github.com/zond/god/dhash"
"github.com/zond/setop"
)

var ran = false

func ExampleSetExpression() {
conn := MustConn("127.0.0.1:9191")
conn.Kill()
server := dhash.NewNodeDir("127.0.0.1:2020", "127.0.0.1:2020", "").MustStart()
defer server.Stop()
conn := client.MustConn("127.0.0.1:2020")
conn.SubPut([]byte("myfriends"), []byte("alice"), common.EncodeFloat64(10))
conn.SubPut([]byte("myfriends"), []byte("bob"), common.EncodeFloat64(5))
conn.SubPut([]byte("yourfriends"), []byte("bob"), common.EncodeFloat64(6))
Expand All @@ -27,8 +33,9 @@ func ExampleSetExpression() {
}

func ExampleTreeMirror() {
conn := MustConn("127.0.0.1:9191")
conn.Kill()
server := dhash.NewNodeDir("127.0.0.1:3030", "127.0.0.1:3030", "").MustStart()
defer server.Stop()
conn := client.MustConn("127.0.0.1:3030")
conn.SubAddConfiguration([]byte("myfriends"), "mirrored", "yes")
conn.SubPut([]byte("myfriends"), []byte("alice"), common.EncodeFloat64(10))
conn.SubPut([]byte("myfriends"), []byte("bob"), common.EncodeFloat64(5))
Expand Down
Binary file added client/examples/examples.test
Binary file not shown.
25 changes: 21 additions & 4 deletions dhash/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package dhash
import (
"bytes"
"fmt"
"github.com/zond/god/client"
"github.com/zond/god/common"
"github.com/zond/god/murmur"
"github.com/zond/setop"
"math/big"
"net"
"runtime"
"testing"
"time"

"github.com/zond/god/client"
"github.com/zond/god/common"
"github.com/zond/god/murmur"
"github.com/zond/setop"
)

type testClient interface {
Expand Down Expand Up @@ -113,6 +114,7 @@ func TestClient(t *testing.T) {
dhashes := testStartup(t, common.Redundancy*2, 11191)
testGOBClient(t, dhashes)
testJSONClient(t, dhashes)
stopServers(dhashes)
}

func clearAll(dhashes []*Node) {
Expand All @@ -122,13 +124,15 @@ func clearAll(dhashes []*Node) {
}

func testGOBClient(t *testing.T, dhashes []*Node) {
fmt.Println(" === Run testGOBClient")
clearAll(dhashes)
c := client.MustConn(dhashes[0].GetBroadcastAddr())
c.Start()
testClientInterface(t, dhashes, c)
}

func testJSONClient(t *testing.T, dhashes []*Node) {
fmt.Println(" === Run testJSONClient")
for _, dhash := range dhashes {
clearAll(dhashes)
addr, err := net.ResolveTCPAddr("tcp", dhash.node.GetBroadcastAddr())
Expand All @@ -141,20 +145,33 @@ func testJSONClient(t *testing.T, dhashes []*Node) {
}

func testClientInterface(t *testing.T, dhashes []*Node, c testClient) {
fmt.Println(" === Run testGetPutDel")
testGetPutDel(t, c)
fmt.Println(" === Run testSubGetPutDel")
testSubGetPutDel(t, c)
fmt.Println(" === Run testSubClear")
testSubClear(t, c)
fmt.Println(" === Run testIndices")
testIndices(t, dhashes, c)
if rc, ok := c.(*client.Conn); ok {
fmt.Println(" === Run testDump")
testDump(t, rc)
fmt.Println(" === Run testSubDump")
testSubDump(t, rc)
}
fmt.Println(" === Run testNextPrev")
testNextPrev(t, c)
fmt.Println(" === Run testCounts")
testCounts(t, dhashes, c)
fmt.Println(" === Run testNextPrevIndices")
testNextPrevIndices(t, dhashes, c)
fmt.Println(" === Run testSlices")
testSlices(t, dhashes, c)
fmt.Println(" === Run testSliceIndices")
testSliceIndices(t, dhashes, c)
fmt.Println(" === Run testSliceLen")
testSliceLen(t, dhashes, c)
fmt.Println(" === Run testSetExpression")
testSetExpression(t, c)
}

Expand Down
18 changes: 15 additions & 3 deletions dhash/json_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os/exec"

"github.com/zond/god/common"
"github.com/zond/setop"
"net/http"
)

// JSONClient is used in the tests to ensure that the JSON API provides roughly the same functionality as the gob API.
// It is also a demonstration and example of how the JSON API can be used.
// It is NOT meant to be used as a real client, since if you are using Go anyway the client.Conn type is much more efficient.
type JSONClient string

var jsonClient = &http.Client{}

var lastOpen int32 = 0

func (self JSONClient) call(action string, params, result interface{}) {
client := new(http.Client)
buf := new(bytes.Buffer)
if params != nil {
err := json.NewEncoder(buf).Encode(params)
Expand All @@ -28,14 +34,20 @@ func (self JSONClient) call(action string, params, result interface{}) {
panic(err)
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
resp, err := jsonClient.Do(req)
if err != nil {
outp, e := exec.Command("sysctl", "kern.num_files").Output()
if e != nil {
panic(e)
}
fmt.Println(string(outp))
panic(err)
}
err = json.NewDecoder(resp.Body).Decode(result)
if err != nil {
panic(err)
}
ioutil.ReadAll(resp.Body)
resp.Body.Close()
}
func (self JSONClient) SSubPut(key, subKey, value []byte) {
Expand Down
17 changes: 14 additions & 3 deletions discord/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package discord
import (
"bytes"
"fmt"
"github.com/zond/god/common"
"github.com/zond/god/murmur"
"net"
"net/rpc"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/zond/god/common"
"github.com/zond/god/murmur"
)

// CommListener is a function listening for generic communication between two Nodes.
Expand Down Expand Up @@ -206,11 +208,20 @@ func (self *Node) Start() (err error) {
}
}
self.ring.Add(self.Remote())
go server.Accept(self.getListener())
go func() {
var conn net.Conn
for conn, err = self.getListener().Accept(); err == nil; conn, err = self.getListener().Accept() {
go server.ServeConn(conn)
}
if !strings.Contains(err.Error(), "use of closed network connection") {
panic(err)
}
}()
go self.notifyPeriodically()
go self.pingPeriodically()
return
}

func (self *Node) notifyPeriodically() {
for self.hasState(started) {
self.notifySuccessor()
Expand Down
5 changes: 3 additions & 2 deletions timenet/timenet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package timenet

import (
"fmt"
"github.com/zond/god/common"
"math"
"math/rand"
"testing"
"time"

"github.com/zond/god/common"
)

func init() {
Expand Down Expand Up @@ -78,5 +79,5 @@ func TestSample(t *testing.T) {
common.AssertWithin(t, func() (string, bool) {
d := producer.deviance()
return fmt.Sprint(d), d > 0 && d < 1000000
}, time.Second*10)
}, time.Second*20)
}

0 comments on commit 1f40693

Please sign in to comment.