Skip to content

Commit

Permalink
Permit convert on values in Store put! methods (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krastanov committed Jun 3, 2024
1 parent 13654da commit d51d781
Show file tree
Hide file tree
Showing 5 changed files with 41 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
34 changes: 34 additions & 0 deletions test/test_resources_stores_cast.jl
Original file line number Diff line number Diff line change
@@ -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.8" && @test_throws "MethodError: Cannot `convert`" runsim(sim->Store{Symbol}(sim))

2 comments on commit d51d781

@Krastanov
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/108149

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.4.1 -m "<description of version>" d51d781d54a1b75efdbd335f6410c10d4607b525
git push origin v1.4.1

Please sign in to comment.