fixed bug

This commit is contained in:
didier amyot committed 2015-07-07 21:02:04 -04:00
1 parent 0c8e5ed294
commit 81bd0d075e
3 files changed
+18 -6

No files matched your search

+5 -1
View File
@@ -39,6 +39,7 @@ type Doorman struct {
LastChangeTimestamp int64 // an always increasing int that represent the last time the doorman has beed updated
Probabilities []*big.Rat // The probability of each cases. The sum of probabilities needs to be one
wg sync.WaitGroup // waitgroup for goroutine safety
hashKey []byte // the decoded id
}
func New(id string, probabilities []*big.Rat) (*Doorman, error) {
@@ -47,6 +48,9 @@ func New(id string, probabilities []*big.Rat) (*Doorman, error) {
return nil, err
} else if len(bid) != 16 {
return nil, errors.New("id must be base64 encoded of a 16 bytes array")
} else {
wab.hashKey = bid
wab.Id = id
}
wab.Probabilities = probabilities
return wab, wab.Validate()
@@ -133,7 +137,7 @@ func (w *Doorman) GenerateRandomProbabilityFromInteger(data uint64) *big.Rat {
}
func (w *Doorman) Hash(data ...[]byte) uint64 {
h := siphash.New(make([]byte, 16))
h := siphash.New(w.hashKey)
for _, datum := range data {
h.Write(datum)
}
+4 -1
View File
@@ -58,7 +58,9 @@ func TestNew(t *testing.T) {
t.Error()
}
goodId := "MTIzNDU2Nzg5MDEyMzQ1Ng=="
if _, err := New(goodId, getProbs("1/4", "1/4")); err == nil {
if dm, err := New(goodId, getProbs("1/4", "1/4")); err == nil {
t.Error()
} else if dm.Id != goodId {
t.Error()
}
}
@@ -131,6 +133,7 @@ func TestGenerateRandomProbabilityFromBitSlice(t *testing.T) {
func TestHash(t *testing.T) {
m := []byte("doormen are great")
w := new(Doorman)
w.hashKey = make([]byte, 16)
var expt uint64 = 0x3973fc1b3e324215
result := w.Hash(m)
+9 -4
View File
@@ -10,7 +10,6 @@ import (
"strconv"
"github.com/didiercrunch/doorman"
"gopkg.in/mgo.v2/bson"
)
func getProbs(probs ...float64) []*big.Rat {
@@ -21,9 +20,13 @@ func getProbs(probs ...float64) []*big.Rat {
return ret
}
var wab = doorman.New(bson.ObjectIdHex("558701a2f6fb5e29a4000003"), getProbs(1, 0, 0))
var wab *doorman.Doorman
func init() {
var err error
if wab, err = doorman.New("zST8_FWcPDT2-14wDQAAAQ==", getProbs(1, 0, 0)); err != nil {
panic(err)
}
if err := wab.Subscriber("http://localhost:1999"); err != nil {
panic(err)
}
@@ -47,13 +50,16 @@ var tmpl = `
<html>
<head></head>
<body style="background:{{ .color }};width:100%;height:100%">
<div style="width: 62.5rem;">
<h1 style="text-align:center;">the doorman value is {{ .value }}</h1>
<p style="text-align:center;">
this doorman depend solely on the url. If you hit different url you
This doorman depend solely on the url. If you hit different url you
will have different doorman results. For example, here is a random
url: <a href="{{ .nextUrl }}" style="color: black;"> {{ .nextUrl }} </a>
</p>
</div>
</body>
</html>
`
@@ -84,5 +90,4 @@ func main() {
port := getPort()
log.Println("serving on port", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}