ingestor_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package cloudcost
  2. import (
  3. "sync"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/opencost"
  7. "github.com/opencost/opencost/pkg/cloud"
  8. )
  9. type fakeIntegration struct{}
  10. func (fakeIntegration) GetCloudCost(time.Time, time.Time) (*opencost.CloudCostSetRange, error) {
  11. return nil, nil
  12. }
  13. func (fakeIntegration) GetStatus() cloud.ConnectionStatus { return cloud.InitialStatus }
  14. func (fakeIntegration) RefreshStatus() cloud.ConnectionStatus { return cloud.InitialStatus }
  15. // TestIngestor_Status_ConcurrentWithCoverageWrite guards against a data race on
  16. // the coverage window: Status() read it without holding coverageLock while the
  17. // build loop reassigns it under the lock. Run with -race to detect it.
  18. func TestIngestor_Status_ConcurrentWithCoverageWrite(t *testing.T) {
  19. now := time.Now().UTC()
  20. ing := &ingestor{
  21. integration: fakeIntegration{},
  22. coverage: opencost.NewClosedWindow(now, now.Add(time.Hour)),
  23. }
  24. window := opencost.NewClosedWindow(now, now.Add(2*time.Hour))
  25. var wg sync.WaitGroup
  26. wg.Add(2)
  27. go func() {
  28. defer wg.Done()
  29. for i := 0; i < 2000; i++ {
  30. ing.expandCoverage(window)
  31. }
  32. }()
  33. go func() {
  34. defer wg.Done()
  35. for i := 0; i < 2000; i++ {
  36. _ = ing.Status()
  37. }
  38. }()
  39. wg.Wait()
  40. }