cleaned up tests

This commit is contained in:
Todd Gruben 2013-12-11 16:20:33 -06:00
parent b7d9f91d2c
commit 648e44ca72
4 changed files with 80 additions and 46 deletions

View file

@ -3,12 +3,10 @@ package index
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"log"
"time"
)
func TestBitmaps(t *testing.T) {
log.Println("HELLO")
Convey("function BitCount should equal method bm.Count()", t, func() {
bm:= CreateRBBitmap()
for i:=uint64(0); i<uint64(4096);i++{

View file

@ -2,7 +2,6 @@ package index
import (
"encoding/json"
"log"
"fmt"
)
@ -26,7 +25,6 @@ type Command interface {
func BuildCommandFactory(req *RequestJSON,decoder *json.Decoder)Command{
log.Println(req)
var result Command
switch req.Request{
@ -36,6 +34,8 @@ func BuildCommandFactory(req *RequestJSON,decoder *json.Decoder)Command{
result= NewUnion(decoder)
case "IntersectCount":
result= NewIntersect(decoder)
case "SetBit":
result= NewSetBit(decoder)
}
return result
}
@ -116,7 +116,13 @@ func (cmd *CmdIntersect) Response()string {
func (cmd *CmdIntersect) ResponseChannel()chan string {
return cmd.result
}
/*
type BitArgs struct {
Bitmap_id uint64
Bit_pos uint64
}
type CmdSetBit struct{
result chan string
id uint64
@ -124,19 +130,21 @@ type CmdSetBit struct{
}
func NewSetBit(decoder *json.Decoder) *CmdSetBit{
var f interface{}
var f BitArgs
decoder.Decode(&f)
m := f.(map[string]interface{})
result:= &CmdSetBit{make(chan string),f["bitmap_id"].(uint64),f["bit_pos"].(uint64)}
result:= &CmdSetBit{make(chan string),f.Bitmap_id,f.Bit_pos}
return result
}
func (cmd *CmdSetBit) Execute(f *Fragment)string {
bitmap := Get(f.impl,cmd.id)
SetBit(bitmap,cmd.bit_pos)
result := BitCount(bm)
return fmt.Sprintf(`{ "value":%d }`,result)
bitmap := f.impl.Get(cmd.id)
val:= SetBit(bitmap,cmd.bit_pos)
m:=0
if val{
m=1
}
result := BitCount(bitmap)
return fmt.Sprintf(`{ "value":%d , "changed":%d}`,result,m)
}
func (cmd *CmdSetBit) QueryType()string {
return "SetBit"
@ -147,4 +155,3 @@ func (cmd *CmdSetBit) Response()string {
func (cmd *CmdSetBit) ResponseChannel()chan string {
return cmd.result
}
*/

View file

@ -60,7 +60,7 @@ func (a *FragmentContainer) RunServer(porti int, closeChannel chan bool,started
type Pilosa interface{
Union([]uint64) IBitmap
Intersect([] uint64) IBitmap
// SetBit(id uint64, bit_pos int64)bool
Get(id uint64 )IBitmap
}
type RequestJSON struct {
@ -91,7 +91,6 @@ func (f *Fragment) ServeFragment() {
}
func handler(w http.ResponseWriter, r *http.Request,fragments map[string]*Fragment) {
log.Println("GOT MESSAGE")
if r.Method == "POST" {
var f RequestJSON
@ -110,7 +109,6 @@ func handler(w http.ResponseWriter, r *http.Request,fragments map[string]*Fragme
output:=`{"Error":"Invalid Fragment"}`
fc,found := fragments[f.Fragment] //f.FragmentIndex<len(fragments){
if found{
log.Println("Sending Request")
// fc := fragments[f.FragmentGuid]
fc.requestChan <- request
output = request.Response()

View file

@ -4,32 +4,36 @@ import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"net/http"
// "encoding/json"
"encoding/json"
"net/http/httptest"
// "io/ioutil"
// "time"
"log"
"fmt"
"strings"
)
func simple(){
msg:=`
{
func simple(id1,id2 int)string {
return fmt.Sprintf(`{
"Request": "UnionCount",
"Fragment": "AAA-BBB-CCC",
"Args": {
"Bitmaps":[1,2,3,4]
"Bitmaps":[%d,%d]
}
}`
/*
log.Println("POSTING:",msg)
resp,err := http.Post("http://localhost:8089", "application/json", strings.NewReader(msg))
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
log.Println(string(body),err)
log.Println(">>>>DONE")
*/
}`,id1,id2)
}
func set_bit(id,pos int)string {
return fmt.Sprintf(`{
"Request": "SetBit",
"Fragment": "AAA-BBB-CCC",
"Args": {
"Bitmap_id":%d,
"Bit_pos": %d
}
}`,id,pos)
}
func sendRequest(msg string, dummy *FragmentContainer) (int,[]byte){
r, err := http.NewRequest("POST", "http://api/foo", strings.NewReader(msg))
if err != nil {
@ -37,26 +41,53 @@ import (
}
w := httptest.NewRecorder()
dummy:=FragmentContainer{make(map[string]*Fragment)}
dummy.AddFragment("general", "25", 0, "AAA-BBB-CCC")
dummy.ServeHTTP(w , r )
log.Printf("%d - %s", w.Code, w.Body.String())
//return w.Code, w.Body.String()
return w.Code, []byte(w.Body.String())
}
func getResult(key string ,s []byte )interface{}{
var f interface{}
err := json.Unmarshal(s, &f)
if err != nil{
log.Println(err)
return nil
}
m := f.(map[string]interface{})
x := m["results"]
o := x.(map[string]interface{})
return o[key]
}
func TestServer(t *testing.T) {
Convey("Run Server", t, func() {
// Stop := make(chan bool)
// Start := make(chan bool)
// go StartServer(":8089",Stop,Start)
// select{
// case <-Start:
simple()
// case <-time.After(time.Duration(5) * time.Second):
// }
// Stop<- true
So(0, ShouldEqual, 0)
dummy:=&FragmentContainer{make(map[string]*Fragment)}
dummy.AddFragment("general", "25", 0, "AAA-BBB-CCC")
var (
c int
s []byte
)
Convey("Set Bit 1 1", t, func() {
c,s=sendRequest(set_bit(1,1),dummy)
So(c, ShouldEqual, 200)
v:=getResult("value",s)
So(v, ShouldEqual, 1)
})
Convey("Set Bit 2 2", t, func() {
c,s=sendRequest(set_bit(2,2),dummy)
So(c, ShouldEqual, 200)
v:=getResult("value",s)
So(v, ShouldEqual, 1)
})
Convey("Union", t, func() {
c,s=sendRequest(simple(1,2),dummy)
So(c, ShouldEqual, 200)
v:=getResult("value",s)
So(v, ShouldEqual, 2)
})
}