dashboards_race_test.go 974 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package api
  2. import (
  3. "sync"
  4. "testing"
  5. "github.com/OliveTin/OliveTin/internal/executor"
  6. )
  7. // TestBuildDefaultDashboardConcurrentWithRebuild guards against a crash when the
  8. // default dashboard is built (e.g. by Init) while a config or entity reload
  9. // rebuilds the action map. Without the read lock in buildDefaultDashboard, the
  10. // Go runtime aborts with "concurrent map iteration and map write"; run with
  11. // -race to see the data race directly.
  12. func TestBuildDefaultDashboardConcurrentWithRebuild(t *testing.T) {
  13. cfg, _, adminUser := buildViewPermissionTestConfig(t)
  14. ex := executor.DefaultExecutor(cfg)
  15. ex.RebuildActionMap()
  16. rr := &DashboardRenderRequest{
  17. AuthenticatedUser: adminUser,
  18. cfg: cfg,
  19. ex: ex,
  20. }
  21. var wg sync.WaitGroup
  22. wg.Add(2)
  23. go func() {
  24. defer wg.Done()
  25. for range 200 {
  26. ex.RebuildActionMap()
  27. }
  28. }()
  29. go func() {
  30. defer wg.Done()
  31. for range 200 {
  32. buildDefaultDashboard(rr)
  33. }
  34. }()
  35. wg.Wait()
  36. }