memory_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package pricing
  2. import (
  3. "sync"
  4. "testing"
  5. "github.com/opencost/opencost/core/pkg/unit"
  6. )
  7. // TestMemoryGetEmpty verifies that a fresh store returns a non-nil, empty set.
  8. func TestMemoryGetEmpty(t *testing.T) {
  9. mps := NewMemoryPricingStore()
  10. ps, err := mps.GetPricingSet(t.Context())
  11. if err != nil {
  12. t.Fatalf("unexpected error: %v", err)
  13. }
  14. if ps == nil {
  15. t.Fatal("expected non-nil pricing set")
  16. }
  17. if !ps.IsEmpty() {
  18. t.Errorf("expected empty pricing set, got %+v", ps)
  19. }
  20. }
  21. // TestMemoryRoundTrip verifies that a set survives a Set/Get round trip while
  22. // being returned as a distinct copy, not the stored pointer.
  23. func TestMemoryRoundTrip(t *testing.T) {
  24. mps := NewMemoryPricingStore()
  25. in := fullPricingSet()
  26. if err := mps.SetPricingSet(t.Context(), in); err != nil {
  27. t.Fatalf("unexpected error: %v", err)
  28. }
  29. out, err := mps.GetPricingSet(t.Context())
  30. if err != nil {
  31. t.Fatalf("unexpected error: %v", err)
  32. }
  33. csIn, err := in.Checksum()
  34. if err != nil {
  35. t.Fatalf("unexpected error: %v", err)
  36. }
  37. csOut, err := out.Checksum()
  38. if err != nil {
  39. t.Fatalf("unexpected error: %v", err)
  40. }
  41. if csIn != csOut {
  42. t.Errorf("round-trip changed contents: %q -> %q", csIn, csOut)
  43. }
  44. if out == in {
  45. t.Error("expected Get to return a copy, not the set pointer")
  46. }
  47. }
  48. // TestMemorySetIsolation verifies that mutating the value passed to Set after
  49. // the call does not affect what the store returns.
  50. func TestMemorySetIsolation(t *testing.T) {
  51. mps := NewMemoryPricingStore()
  52. in := fullPricingSet()
  53. if err := mps.SetPricingSet(t.Context(), in); err != nil {
  54. t.Fatalf("unexpected error: %v", err)
  55. }
  56. before, err := mps.GetPricingSet(t.Context())
  57. if err != nil {
  58. t.Fatalf("unexpected error: %v", err)
  59. }
  60. csBefore, err := before.Checksum()
  61. if err != nil {
  62. t.Fatalf("unexpected error: %v", err)
  63. }
  64. // Mutate the caller's copy after setting it.
  65. in.NodePricing[0].Properties.Labels["team"] = "mutated"
  66. in.NodePricing[0].Prices[ResourceNode] = Price{Unit: unit.Hour, Price: 99}
  67. in.NodePricing = append(in.NodePricing, nodePricing("m5.xlarge", 0.192))
  68. after, err := mps.GetPricingSet(t.Context())
  69. if err != nil {
  70. t.Fatalf("unexpected error: %v", err)
  71. }
  72. csAfter, err := after.Checksum()
  73. if err != nil {
  74. t.Fatalf("unexpected error: %v", err)
  75. }
  76. if csBefore != csAfter {
  77. t.Errorf("mutating the set argument changed store contents: %q -> %q", csBefore, csAfter)
  78. }
  79. }
  80. // TestMemoryGetIsolation verifies that mutating a value returned by Get does not
  81. // affect what subsequent Get calls return.
  82. func TestMemoryGetIsolation(t *testing.T) {
  83. mps := NewMemoryPricingStore()
  84. if err := mps.SetPricingSet(t.Context(), fullPricingSet()); err != nil {
  85. t.Fatalf("unexpected error: %v", err)
  86. }
  87. got, err := mps.GetPricingSet(t.Context())
  88. if err != nil {
  89. t.Fatalf("unexpected error: %v", err)
  90. }
  91. csBefore, err := got.Checksum()
  92. if err != nil {
  93. t.Fatalf("unexpected error: %v", err)
  94. }
  95. // Mutate the returned copy.
  96. got.NodePricing[0].Properties.Labels["team"] = "mutated"
  97. got.NodePricing[0].Prices[ResourceNode] = Price{Unit: unit.Hour, Price: 99}
  98. again, err := mps.GetPricingSet(t.Context())
  99. if err != nil {
  100. t.Fatalf("unexpected error: %v", err)
  101. }
  102. csAfter, err := again.Checksum()
  103. if err != nil {
  104. t.Fatalf("unexpected error: %v", err)
  105. }
  106. if csBefore != csAfter {
  107. t.Errorf("mutating a returned set changed store contents: %q -> %q", csBefore, csAfter)
  108. }
  109. }
  110. // TestMemorySetNil verifies that Set rejects a nil pricing set.
  111. func TestMemorySetNil(t *testing.T) {
  112. mps := NewMemoryPricingStore()
  113. if err := mps.SetPricingSet(t.Context(), nil); err == nil {
  114. t.Error("expected error for nil pricing set")
  115. }
  116. }
  117. // TestMemoryConcurrentAccess exercises the store under concurrent readers and
  118. // writers; run with -race to catch data races.
  119. func TestMemoryConcurrentAccess(t *testing.T) {
  120. mps := NewMemoryPricingStore()
  121. var wg sync.WaitGroup
  122. for i := 0; i < 50; i++ {
  123. wg.Add(2)
  124. go func() {
  125. defer wg.Done()
  126. _ = mps.SetPricingSet(t.Context(), fullPricingSet())
  127. }()
  128. go func() {
  129. defer wg.Done()
  130. if _, err := mps.GetPricingSet(t.Context()); err != nil {
  131. t.Errorf("unexpected error: %v", err)
  132. }
  133. }()
  134. }
  135. wg.Wait()
  136. }