Skip to content

Commit

Permalink
Permit convert on values in Store put! methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Krastanov committed Jun 3, 2024
1 parent 3070bc3 commit 034c949
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/resources/stores.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 33 additions & 0 deletions test/test_resources_stores_cast.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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))

@test_throws "MethodError: Cannot `convert`" runsim(sim->Store{Symbol}(sim))

0 comments on commit 034c949

Please sign in to comment.