|
| 1 | +package redisq |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "github.com/DudeWhoCode/kulay/backend" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestPut(t *testing.T) { |
| 11 | + host := "localhost" |
| 12 | + port := "6379" |
| 13 | + pass := "" |
| 14 | + db := 0 |
| 15 | + queue := "testput" |
| 16 | + testCnt := 5 |
| 17 | + client := backend.NewRedisSession(host, port, pass, db) |
| 18 | + client.Del(queue) |
| 19 | + pipe := make(chan string, testCnt) |
| 20 | + type test struct { |
| 21 | + Name string `json:"name"` |
| 22 | + Desc string `json:"desc"` |
| 23 | + Url string `json:"url"` |
| 24 | + Stars int `json:"stars"` |
| 25 | + } |
| 26 | + testData := &test{ |
| 27 | + "kulay", |
| 28 | + "High speed message routing between services", |
| 29 | + "https://github.com/kulay", |
| 30 | + 135, |
| 31 | + } |
| 32 | + testStr, _ := json.Marshal(testData) |
| 33 | + for i := 1; i <= testCnt; i++ { |
| 34 | + pipe <- string(testStr) |
| 35 | + } |
| 36 | + go Put(host, port, pass, db, queue, pipe) |
| 37 | + // Wait until Put populates redis queue |
| 38 | + // TODO : Implement timeout for channels |
| 39 | + time.Sleep(1 * time.Second) |
| 40 | + queueLen, err := client.LLen(queue).Result() |
| 41 | + if err != nil { |
| 42 | + t.Errorf("Expected no error, got %s", err) |
| 43 | + } |
| 44 | + if int(queueLen) != testCnt { |
| 45 | + t.Errorf("Expected message count in redis queue is %v, got %v", testCnt, queueLen) |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +func TestGet(t *testing.T) { |
| 51 | + host := "localhost" |
| 52 | + port := "6379" |
| 53 | + pass := "" |
| 54 | + db := 0 |
| 55 | + queue := "testget" |
| 56 | + testCnt := 5 |
| 57 | + client := backend.NewRedisSession(host, port, pass, db) |
| 58 | + client.Del(queue) |
| 59 | + pipe := make(chan string, testCnt) |
| 60 | + type test struct { |
| 61 | + Name string `json:"name"` |
| 62 | + Desc string `json:"desc"` |
| 63 | + Url string `json:"url"` |
| 64 | + Stars int `json:"stars"` |
| 65 | + } |
| 66 | + testData := &test{ |
| 67 | + "kulay", |
| 68 | + "High speed message routing between services", |
| 69 | + "https://github.com/kulay", |
| 70 | + 135, |
| 71 | + } |
| 72 | + testStr, _ := json.Marshal(testData) |
| 73 | + for i := 1; i <= testCnt; i++ { |
| 74 | + if err := client.RPush(queue, testStr).Err(); err != nil { |
| 75 | + t.Fatalf("Expected no error while pushing messages, got %s", err) |
| 76 | + } |
| 77 | + } |
| 78 | + Get(host, port, pass, db, queue, pipe) |
| 79 | + if len(pipe) != testCnt { |
| 80 | + t.Errorf("Expected message count in channel is %v, got %v", testCnt, len(pipe)) |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments