Skip to content

Commit

Permalink
Fix Julia 1.12 allocation tests (#482)
Browse files Browse the repository at this point in the history
* Wait for cache lock during copy
* Avoid using legacy time zone in tests
* Make allocation tests more reliable on Julia 1.12
  • Loading branch information
omus authored Dec 20, 2024
1 parent 6d0d11a commit 041974d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/types/timezonecache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ TimeZoneCache() = TimeZoneCache(Dict(), Dict(), ReentrantLock(), Threads.Atomic{
const _TZ_CACHE = TimeZoneCache()

function Base.copy!(dst::TimeZoneCache, src::TimeZoneCache)
copy!(dst.ftz, src.ftz)
copy!(dst.vtz, src.vtz)
dst.initialized[] = src.initialized[]
lock(dst.lock) do
copy!(dst.ftz, src.ftz)
copy!(dst.vtz, src.vtz)
dst.initialized[] = src.initialized[]
end
return dst
end

Expand Down
11 changes: 11 additions & 0 deletions test/helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,14 @@ function with_tz_cache(f, cache::TimeZones.TimeZoneCache)
copy!(TimeZones._TZ_CACHE, old_cache)
end
end

function with_tz_cache(f)
old_cache = deepcopy(TimeZones._TZ_CACHE)
copy!(TimeZones._TZ_CACHE, TimeZones.TimeZoneCache())

try
return withenv(f, "JULIA_TZDATA_VERSION" => TZDATA_VERSION)
finally
copy!(TimeZones._TZ_CACHE, old_cache)
end
end
5 changes: 3 additions & 2 deletions test/plotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# lets use avoid a Plots.jl dependency and issues with running that during tests.
# `RecipesBase.apply_recipe` is not a documented API, but is fairly usable.
# Comments above each use show the matching `plot` function command
start_zdt = ZonedDateTime(2017,1,1,0,0,0, tz"EST")
end_zdt = ZonedDateTime(2017,1,1,10,30,0, tz"EST")
tz = FixedTimeZone("EST", -18000)
start_zdt = ZonedDateTime(2017,1,1,0,0,0, tz)
end_zdt = ZonedDateTime(2017,1,1,10,30,0, tz)
zoned_dates = start_zdt:Hour(1):end_zdt

# what the point should be after recipe is applied
Expand Down
26 changes: 17 additions & 9 deletions test/types/timezone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ end
@test TimeZone("Etc/GMT-14", Class(:LEGACY)) == FixedTimeZone("Etc/GMT-14", 14 * 3600)
end

# These allocation tests are a bit fragile. Clearing the cache makes these tests more
# in on Julia 1.12.0-DEV.1786.
@testset "allocations" begin
tz = TimeZone("UTC") # Trigger compilation and ensure the cache is populated
@test tz isa FixedTimeZone
@test @allocations(TimeZone("UTC")) == 0
@test @allocations(istimezone("UTC")) == 0

tz = TimeZone("America/Winnipeg") # Trigger compilation and ensure the cache is populated
@test tz isa VariableTimeZone
@test @allocations(TimeZone("America/Winnipeg")) == 2
@test @allocations(istimezone("America/Winnipeg")) == 1
with_tz_cache() do
# Trigger compilation (only upon the first call in Julia) and populate the cache
@test @allocations(TimeZone("UTC")) > 0

@test @allocations(TimeZone("UTC")) == 0
@test @allocations(istimezone("UTC")) == 0
end

with_tz_cache() do
# Trigger compilation (only upon the first call in Julia) and populate the cache
@test @allocations(TimeZone("America/Winnipeg")) > 0

@test @allocations(TimeZone("America/Winnipeg")) == 2
@test @allocations(istimezone("America/Winnipeg")) == 1
end
end

0 comments on commit 041974d

Please sign in to comment.