| 1 |
package hashtag |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/rand" |
| 5 |
"testing" |
| 6 |
|
| 7 |
. "github.com/onsi/ginkgo" |
| 8 |
. "github.com/onsi/gomega" |
| 9 |
) |
| 10 |
|
| 11 |
func TestGinkgoSuite(t *testing.T) { |
| 12 |
RegisterFailHandler(Fail) |
| 13 |
RunSpecs(t, "hashtag") |
| 14 |
} |
| 15 |
|
| 16 |
var _ = Describe("CRC16", func() { |
| 17 |
|
| 18 |
// http://redis.io/topics/cluster-spec#keys-distribution-model |
| 19 |
It("should calculate CRC16", func() { |
| 20 |
tests := []struct { |
| 21 |
s string |
| 22 |
n uint16 |
| 23 |
}{ |
| 24 |
{"123456789", 0x31C3}, |
| 25 |
{string([]byte{83, 153, 134, 118, 229, 214, 244, 75, 140, 37, 215, 215}), 21847}, |
| 26 |
} |
| 27 |
|
| 28 |
for _, test := range tests { |
| 29 |
Expect(crc16sum(test.s)).To(Equal(test.n), "for %s", test.s) |
| 30 |
} |
| 31 |
}) |
| 32 |
|
| 33 |
}) |
| 34 |
|
| 35 |
var _ = Describe("HashSlot", func() { |
| 36 |
|
| 37 |
It("should calculate hash slots", func() { |
| 38 |
tests := []struct { |
| 39 |
key string |
| 40 |
slot int |
| 41 |
}{ |
| 42 |
{"123456789", 12739}, |
| 43 |
{"{}foo", 9500}, |
| 44 |
{"foo{}", 5542}, |
| 45 |
{"foo{}{bar}", 8363}, |
| 46 |
{"", 10503}, |
| 47 |
{"", 5176}, |
| 48 |
{string([]byte{83, 153, 134, 118, 229, 214, 244, 75, 140, 37, 215, 215}), 5463}, |
| 49 |
} |
| 50 |
// Empty keys receive random slot. |
| 51 |
rand.Seed(100) |
| 52 |
|
| 53 |
for _, test := range tests { |
| 54 |
Expect(Slot(test.key)).To(Equal(test.slot), "for %s", test.key) |
| 55 |
} |
| 56 |
}) |
| 57 |
|
| 58 |
It("should extract keys from tags", func() { |
| 59 |
tests := []struct { |
| 60 |
one, two string |
| 61 |
}{ |
| 62 |
{"foo{bar}", "bar"}, |
| 63 |
{"{foo}bar", "foo"}, |
| 64 |
{"{user1000}.following", "{user1000}.followers"}, |
| 65 |
{"foo{{bar}}zap", "{bar"}, |
| 66 |
{"foo{bar}{zap}", "bar"}, |
| 67 |
} |
| 68 |
|
| 69 |
for _, test := range tests { |
| 70 |
Expect(Slot(test.one)).To(Equal(Slot(test.two)), "for %s <-> %s", test.one, test.two) |
| 71 |
} |
| 72 |
}) |
| 73 |
|
| 74 |
}) |