hashtag_test.go 1.5 KB

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