github.com/orofarne/hammy@v0.0.0-20130409105742-374fadfd6ecb/src/hammy/send_buffer_test.go (about) 1 package hammy 2 3 import ( 4 "testing" 5 //"fmt" 6 "time" 7 "encoding/json" 8 ) 9 10 func TestSendBufferSimple(t *testing.T) { 11 rh := RequestHandlerTestImpl{} 12 var cfg Config 13 cfg.SendBuffer.SleepTime = 0.1 14 sb := NewSendBufferImpl(&rh, cfg, "send_buffer_impl_test_instance") 15 16 json1 := `{ 17 "host1": { 18 "key1.1": [{ 19 "timestamp": 1361785778, 20 "value": 3.14 21 }] 22 }, 23 "host2": { 24 "key2.1": [{ 25 "timestamp": 1361785817, 26 "value": "test string" 27 }] 28 } 29 }` 30 json2 := `{ 31 "host2": { 32 "key2.1": [{ 33 "timestamp": 1361785819, 34 "value": "test string 2" 35 }], 36 "key2.2": [{ 37 "timestamp": 1361785858, 38 "value": 12345 39 }, 40 { 41 "timestamp": 1361785927, 42 "value": 999.3 43 }] 44 }, 45 "host3": { 46 "key3.1": [{ 47 "timestamp": 1361785788, 48 "value": 77.0 49 }] 50 } 51 }` 52 53 var data1, data2 IncomingData 54 if err := json.Unmarshal([]byte(json1), &data1); err != nil { 55 panic(err) 56 } 57 if err := json.Unmarshal([]byte(json2), &data2); err != nil { 58 panic(err) 59 } 60 61 go sb.Listen() 62 sb.Push(&data1) 63 sb.Push(&data2) 64 65 //fmt.Printf("[send_buffer_test.go] Sleeping...\n") 66 67 time.Sleep(200 * time.Millisecond) 68 69 // Check data 70 if host1, found := rh.Data["host1"]; !found { 71 t.Errorf("`host1` not found") 72 } else { 73 if key11, found := host1["key1.1"]; !found { 74 t.Errorf("`key1.1` not found") 75 } else { 76 if len(key11) != 1 { 77 t.Errorf("Expected len(key11) = 1, got: %d", len(key11)) 78 } else { 79 if key11[0].Timestamp != 1361785778 { 80 t.Errorf("Expected %v, got %#v", 1361785778, key11[0].Timestamp) 81 } 82 val, converted := key11[0].Value.(float64) 83 if !converted { t.Errorf("Wrong type %T", key11[0].Value) } else { 84 if val != 3.14 { 85 t.Errorf("Expected %v, got %#v", 3.14, val) 86 } 87 } 88 } 89 } 90 } 91 if host2, found := rh.Data["host2"]; !found { 92 t.Errorf("`host2` not found") 93 } else { 94 if key21, found := host2["key2.1"]; !found { 95 t.Errorf("`key2.1` not found") 96 } else { 97 if len(key21) != 2 { 98 t.Errorf("Expected len(key21) = 2, got: %d", len(key21)) 99 } else { 100 if key21[0].Timestamp != 1361785817 { 101 t.Errorf("Expected %v, got %#v", 1361785817, key21[0].Timestamp) 102 } 103 val, converted := key21[0].Value.(string) 104 if !converted { t.Errorf("Wrong type %T", key21[0].Value) } else { 105 if val != "test string" { 106 t.Errorf("Expected %v, got %#v", "test string", val) 107 } 108 } 109 if key21[1].Timestamp != 1361785819 { 110 t.Errorf("Expected %v, got %#v", 1361785819, key21[1].Timestamp) 111 } 112 val, converted = key21[1].Value.(string) 113 if !converted { t.Errorf("Wrong type %T", key21[1].Value) } else { 114 if val != "test string 2" { 115 t.Errorf("Expected %v, got %#v", "test string 2", val) 116 } 117 } 118 } 119 } 120 if key22, found := host2["key2.2"]; !found { 121 t.Errorf("`key2.2` not found") 122 } else { 123 if len(key22) != 2 { 124 t.Errorf("Expected len(key22) = 2, got: %d", len(key22)) 125 } else { 126 if key22[0].Timestamp != 1361785858 { 127 t.Errorf("Expected %v, got %#v", 1361785858, key22[0].Timestamp) 128 } 129 val, converted := key22[0].Value.(float64) 130 if !converted { t.Errorf("Wrong type %T", key22[0].Value) } else { 131 if val != 12345 { 132 t.Errorf("Expected %v, got %#v", 12345, val) 133 } 134 } 135 if key22[1].Timestamp != 1361785927 { 136 t.Errorf("Expected %v, got %#v", 1361785927, key22[1].Timestamp) 137 } 138 val, converted = key22[1].Value.(float64) 139 if !converted { t.Errorf("Wrong type %T", key22[1].Value) } else { 140 if val != 999.3 { 141 t.Errorf("Expected %v, got %#v", 999.3, val) 142 } 143 } 144 } 145 } 146 } 147 if host3, found := rh.Data["host3"]; !found { 148 t.Errorf("`host3` not found") 149 } else { 150 if key31, found := host3["key3.1"]; !found { 151 t.Errorf("`key3.1` not found") 152 } else { 153 if len(key31) != 1 { 154 t.Errorf("Expected len(key31) = 1, got: %d", len(key31)) 155 } else { 156 if key31[0].Timestamp != 1361785788 { 157 t.Errorf("Expected %v, got %#v", 1361785788, key31[0].Timestamp) 158 } 159 val, converted := key31[0].Value.(float64) 160 if !converted { t.Errorf("Wrong type %T", key31[0].Value) } else { 161 if val != 77.0 { 162 t.Errorf("Expected %v, got %#v", 77.0, val) 163 } 164 } 165 } 166 } 167 } 168 169 170 }