reader_event_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package uhf
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func TestChannelEventsUseMonotonicIDs(t *testing.T) {
  8. channelEventMu.Lock()
  9. previousQueue := ChannelEventQueue
  10. previousNextID := channelEventNextID
  11. ChannelEventQueue = make([]ChannelEvent, 0, 50)
  12. channelEventNextID = 0
  13. channelEventMu.Unlock()
  14. t.Cleanup(func() {
  15. channelEventMu.Lock()
  16. ChannelEventQueue = previousQueue
  17. channelEventNextID = previousNextID
  18. channelEventMu.Unlock()
  19. })
  20. PushChannelEvent(ChannelEvent{RFIDTag: "A", Timestamp: 100})
  21. PushChannelEvent(ChannelEvent{RFIDTag: "B", Timestamp: 100})
  22. all := GetChannelEvents(0)
  23. require.Len(t, all, 2)
  24. require.Equal(t, uint64(1), all[0].ID)
  25. require.Equal(t, uint64(2), all[1].ID)
  26. afterFirst := GetChannelEvents(all[0].ID)
  27. require.Len(t, afterFirst, 1)
  28. require.Equal(t, "B", afterFirst[0].RFIDTag)
  29. }
  30. func TestDeviceHandlerAcceptReportDeduplicatesDeviceAndEPC(t *testing.T) {
  31. h := &DeviceHandler{lastReport: make(map[string]time.Time)}
  32. base := time.Unix(100, 0)
  33. report := &ReportData{DeviceCode: "RFID-01", Epcs: []string{"e20001"}}
  34. if !h.acceptReport(report, base) {
  35. t.Fatal("first report should be accepted")
  36. }
  37. if h.acceptReport(report, base.Add(reportDebounceWindow-time.Nanosecond)) {
  38. t.Fatal("duplicate report inside debounce window should be rejected")
  39. }
  40. if !h.acceptReport(report, base.Add(reportDebounceWindow)) {
  41. t.Fatal("report at the end of debounce window should be accepted")
  42. }
  43. }
  44. func TestDeviceHandlerAcceptReportAllowsDifferentEPCs(t *testing.T) {
  45. h := &DeviceHandler{lastReport: make(map[string]time.Time)}
  46. now := time.Unix(200, 0)
  47. if !h.acceptReport(&ReportData{DeviceCode: "RFID-01", Epcs: []string{"e20001"}}, now) {
  48. t.Fatal("first EPC should be accepted")
  49. }
  50. if !h.acceptReport(&ReportData{DeviceCode: "RFID-01", Epcs: []string{"e20002"}}, now) {
  51. t.Fatal("different EPC should be accepted")
  52. }
  53. if !h.acceptReport(&ReportData{DeviceCode: "RFID-02", Epcs: []string{"e20001"}}, now) {
  54. t.Fatal("same EPC from a different device should be accepted")
  55. }
  56. }