many many things
This commit is contained in:
1 parent
fb9084794c
commit
af69e728a2
9 files changed
+253
-59
No files matched your search
@@ -0,0 +1,54 @@
|
||||
package httpsubscriber
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/didiercrunch/doorman/shared"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
type HttpSubscriber struct {
|
||||
Url string
|
||||
HartBeat time.Duration
|
||||
}
|
||||
|
||||
func (s *HttpSubscriber) GetDoormanUpdater() (*shared.DoormanUpdater, error) {
|
||||
resp, err := http.Get(s.Url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if resp.StatusCode != 200 {
|
||||
return nil, errors.New("bad http status when GETting update, " + resp.Status)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
de := json.NewDecoder(resp.Body)
|
||||
ret := new(shared.DoormanUpdater)
|
||||
err = de.Decode(ret)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (s *HttpSubscriber) callUpdateHandlerFunction(f shared.UpdateHandlerFunc, data []byte) error {
|
||||
wu := &shared.DoormanUpdater{}
|
||||
if err := json.Unmarshal(data, wu); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return f(wu)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *HttpSubscriber) Subscribe(abtestId bson.ObjectId, update shared.UpdateHandlerFunc) error {
|
||||
go func() {
|
||||
c := time.Tick(s.HartBeat)
|
||||
for _ = range c {
|
||||
if du, err := s.GetDoormanUpdater(); err != nil {
|
||||
log.Printf("error retrieving doorman %v \n%v\n", abtestId, err)
|
||||
} else {
|
||||
update(du)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package httpsubscriber
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/didiercrunch/doorman/shared"
|
||||
)
|
||||
|
||||
func TestCallUpdateHandlerFunction(t *testing.T) {
|
||||
f := func(m *shared.DoormanUpdater) error {
|
||||
if m.Id != "foo" {
|
||||
t.Error("bad doorman id", m.Id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := json.Marshal(&shared.DoormanUpdater{Id: "foo"})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if new(HttpSubscriber).callUpdateHandlerFunction(f, data); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func MockEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
du := &shared.DoormanUpdater{
|
||||
"b64",
|
||||
897987,
|
||||
[]*big.Rat{big.NewRat(1, 4), big.NewRat(3, 4)},
|
||||
}
|
||||
enc := json.NewEncoder(w)
|
||||
if err := enc.Encode(du); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestGetDoormanUpdater(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(MockEndpoint))
|
||||
defer ts.Close()
|
||||
|
||||
s := HttpSubscriber{Url: ts.URL}
|
||||
if d, err := s.GetDoormanUpdater(); err != nil {
|
||||
t.Error(err)
|
||||
} else if d.Id != "b64" {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user