From d42bb88920afa7eb6cf51e57e69b4f449a1e6375 Mon Sep 17 00:00:00 2001 From: Stefan Krastanov Date: Mon, 3 Jun 2024 11:10:42 -0400 Subject: [PATCH] Permit `convert` on values in `Store` `put!` methods --- CHANGELOG.md | 4 +++- Project.toml | 2 +- src/resources/stores.jl | 2 ++ test/runtests.jl | 1 + test/test_resources_stores_cast.jl | 34 ++++++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100755 test/test_resources_stores_cast.jl diff --git a/CHANGELOG.md b/CHANGELOG.md index 736bbf7..d2aca90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ # News -## v1.4.1 - dev +## v1.4.1 - 2024-05-03 + +- Permit stores `put!` methods to cast the value being placed to the type appropriate for the given store. - Added examples to the documentation that were lost around the time of the rewrite for v0.5 in 2018. ## v1.4.0 - 2023-08-07 diff --git a/Project.toml b/Project.toml index ce7bb46..4db9c5d 100644 --- a/Project.toml +++ b/Project.toml @@ -5,7 +5,7 @@ license = "MIT" desc = "A discrete event process oriented simulation framework." authors = ["Ben Lauwens and SimJulia and ConcurrentSim contributors"] repo = "https://github.com/JuliaDynamics/ConcurrentSim.jl.git" -version = "1.4.0" +version = "1.4.1" [deps] DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" diff --git a/src/resources/stores.jl b/src/resources/stores.jl index 67e86fd..5e7aa23 100755 --- a/src/resources/stores.jl +++ b/src/resources/stores.jl @@ -68,6 +68,8 @@ function put!(sto::Store{N, T}, item::N; priority=zero(T)) where {N, T<:Number} put_ev end +put!(sto::Store{N, T}, item; priority=zero(T)) where {N, T<:Number} = put!(sto, convert(N, item); priority) + get_any_item(::N) where N = true function get(sto::Store{N, T, D}, filter::Function=get_any_item; priority=zero(T)) where {N, T<:Number, D} diff --git a/test/runtests.jl b/test/runtests.jl index 8d7ec0a..bd93b9f 100755 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -34,6 +34,7 @@ println("Starting tests with $(Threads.nthreads()) threads out of `Sys.CPU_THREA @doset "resources_containers" @doset "resources_containers_deprecated" @doset "resources_stores" +@doset "resources_stores_cast" @doset "resources_stores_deprecated" @doset "resources_fancy_stores" @doset "resource_priorities" diff --git a/test/test_resources_stores_cast.jl b/test/test_resources_stores_cast.jl new file mode 100755 index 0000000..6396f38 --- /dev/null +++ b/test/test_resources_stores_cast.jl @@ -0,0 +1,34 @@ +using ConcurrentSim +using ResumableFunctions +using Test + +@resumable function producer(env, queue) + for item in [1,2,3,4] + @info "putting $item at time $(now(env))" + put!(queue, item) + @yield timeout(env, 2) + end +end +@resumable function consumer(env, queue) + @yield timeout(env, 5) + while true + t = @yield take!(queue) + @test isa(t, Float64) + @info "taking $(t) at time $(now(env))" + end +end + +function runsim(storeconstructor) + sim = Simulation() + queue = storeconstructor(sim) + @process producer(sim, queue) + @process consumer(sim, queue) + run(sim, 30) +end + +runsim(sim->DelayQueue{Float64}(sim, 10)) +runsim(sim->QueueStore{Float64}(sim)) +runsim(sim->Store{Float64}(sim)) + +# formatting was different in older versions +VERSION > v"1.7" && @test_throws "MethodError: Cannot `convert`" runsim(sim->Store{Symbol}(sim))