beacon_race_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package httpapi
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. )
  7. // TestBeaconFDBIngestNoRaceOnSystem cubre la carrera que introdujo #2134: el
  8. // datagrama FDB reutiliza el System del payload anterior, ya publicado, e
  9. // Ingest normalizaba las MAC escribiendo en él mientras otras goroutines lo
  10. // leían. Solo detecta la carrera con el detector:
  11. //
  12. // go test -race ./internal/httpapi/ -run TestBeaconFDBIngestNoRaceOnSystem
  13. func TestBeaconFDBIngestNoRaceOnSystem(t *testing.T) {
  14. s, token := newBeaconTestServer(t)
  15. // 1) Beacon periódico inicial con fw/up/mac: crea prev.Data.System en el registry.
  16. initPkt := fmt.Sprintf(
  17. `{"v":1,"slug":"switch16","token":%q,"seq":1,"up":120,"fw":"1.0.0","mac":"78D800323149","ports":[{"n":1,"l":2}]}`,
  18. token,
  19. )
  20. s.ingestBeacon("192.168.1.20", []byte(initPkt))
  21. if p, ok := s.agents.Fresh("switch16"); !ok || p == nil || p.Data.System == nil {
  22. t.Fatalf("esperaba payload inicial con Data.System tras el primer beacon")
  23. }
  24. fdbPkt := []byte(fmt.Sprintf(
  25. `{"v":1,"slug":"switch16","token":%q,"seq":2,"fdb":{"AABBCCDDEE01":"lan1"}}`,
  26. token,
  27. ))
  28. var wg sync.WaitGroup
  29. stop := make(chan struct{})
  30. // Lector concurrente que simula Live.polledFromAgent / GET /api/agents.
  31. wg.Add(1)
  32. go func() {
  33. defer wg.Done()
  34. for {
  35. select {
  36. case <-stop:
  37. return
  38. default:
  39. if p, ok := s.agents.Fresh("switch16"); ok && p != nil && p.Data.System != nil {
  40. if p.Data.System.BridgeMAC == "" {
  41. t.Errorf("esperaba BridgeMAC no vacía")
  42. }
  43. }
  44. }
  45. }
  46. }()
  47. // Datagramas FDB repetidos: cada uno reinyecta prev.Data.System en
  48. // fdbPayload y llama a s.agents.Ingest(fdbPayload).
  49. for i := 0; i < 50; i++ {
  50. s.ingestBeacon("192.168.1.20", fdbPkt)
  51. }
  52. close(stop)
  53. wg.Wait()
  54. }