| 1 | package calendartest |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "sync" |
| 6 | "testing" |
| 7 | |
| 8 | calendar "amadan.net/oficina/calendar/internal/calendar" |
| 9 | ) |
| 10 | |
| 11 | // TestZoneIsSafeUnderConcurrentGarbage drives Zone() the way the |
| 12 | // public booking page will: many requests at once, most of them asking |
| 13 | // for names nobody has ever heard of. |
| 14 | // |
| 15 | // Two things must hold. The cache must survive concurrent use — run |
| 16 | // this under -race — and a name that failed to load must not be kept, |
| 17 | // or a stranger with a ?zone= parameter grows the map without bound. |
| 18 | func TestZoneIsSafeUnderConcurrentGarbage(t *testing.T) { |
| 19 | var wg sync.WaitGroup |
| 20 | for i := 0; i < 100; i++ { |
| 21 | wg.Add(1) |
| 22 | go func(i int) { |
| 23 | defer wg.Done() |
| 24 | if _, err := calendar.Zone(fmt.Sprintf("Nowhere/Town%d", i)); err == nil { |
| 25 | t.Errorf("Nowhere/Town%d resolved", i) |
| 26 | } |
| 27 | if _, err := calendar.Zone("Europe/Dublin"); err != nil { |
| 28 | t.Errorf("Europe/Dublin: %v", err) |
| 29 | } |
| 30 | }(i) |
| 31 | } |
| 32 | wg.Wait() |
| 33 | |
| 34 | // The bound leaves room for the handful of real zones other tests |
| 35 | // in this package load; a hundred garbage names cannot fit inside it. |
| 36 | if n, limit := calendar.ZoneCacheSizeForTest(), len(calendar.CommonZones)+5; n > limit { |
| 37 | t.Errorf("zone cache holds %d entries after 100 garbage names (limit %d): failures are being cached", n, limit) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestIsCommonZoneKnowsTheList(t *testing.T) { |
| 42 | if !calendar.IsCommonZone("Europe/Dublin") { |
| 43 | t.Error("Europe/Dublin is not common") |
| 44 | } |
| 45 | if calendar.IsCommonZone("Nowhere/Town1") { |
| 46 | t.Error("Nowhere/Town1 is common") |
| 47 | } |
| 48 | if calendar.IsCommonZone("") { |
| 49 | t.Error("the empty name is common") |
| 50 | } |
| 51 | } |
| 52 | |