recorder_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package mongo
  2. import (
  3. "sparrow/pkg/protocol"
  4. "sparrow/pkg/rpcs"
  5. "sparrow/pkg/tlv"
  6. "reflect"
  7. "testing"
  8. "time"
  9. )
  10. func TestRecorder(t *testing.T) {
  11. r, err := NewRecorder("localhost", "pandocloud", "commands")
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. tlvs, err := tlv.MakeTLVs([]interface{}{float64(0.1), int64(100), uint64(200)})
  16. if err != nil {
  17. t.Error(err)
  18. }
  19. deviceid := uint64(12345)
  20. timestamp := uint64(time.Now().Unix() * 1000)
  21. subdata := protocol.SubData{
  22. Head: protocol.SubDataHead{1, 2, 3},
  23. Params: tlvs,
  24. }
  25. subdatas := []protocol.SubData{}
  26. subdatas = append(subdatas, subdata)
  27. data := rpcs.ArgsOnStatus{
  28. DeviceId: deviceid,
  29. Timestamp: timestamp,
  30. Subdata: subdatas,
  31. }
  32. err = r.Insert(data)
  33. if err != nil {
  34. t.Error(err)
  35. }
  36. readData := rpcs.ArgsOnStatus{}
  37. err = r.FindLatest(deviceid, &readData)
  38. if err != nil {
  39. t.Error(err)
  40. }
  41. if !reflect.DeepEqual(data, readData) {
  42. t.Errorf("read data want %v, but got %v", data, readData)
  43. }
  44. readDatas := []rpcs.ArgsOnStatus{}
  45. err = r.FindByTimestamp(deviceid, timestamp, timestamp, &readDatas)
  46. t.Log(readDatas)
  47. if !reflect.DeepEqual(data, readDatas[0]) {
  48. t.Errorf("read data by timestamp want %v, but got %v", data, readDatas[0])
  49. }
  50. }