-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gets resource keep/release up and running
- Loading branch information
Showing
2 changed files
with
142 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,81 @@ | ||
defmodule ZiglerTest.Resource.KeepTest do | ||
use ZiglerTest.IntegrationCase, async: true | ||
|
||
@moduletag :resource | ||
test "restore" | ||
|
||
#use Zig, otp_app: :zigler, resources: [:PidResource] | ||
# | ||
#~Z""" | ||
#const beam = @import("beam"); | ||
#const std = @import("std"); | ||
#const Resource = beam.Resource; | ||
# | ||
#pub const PidResource = Resource(beam.pid, @import("root"), .{.Callbacks = PidResourceCallbacks}); | ||
# | ||
#pub const PidResourceCallbacks = struct { | ||
# pub fn dtor(pid: *beam.pid) void { | ||
# _ = beam.send(pid.*, .cleaned, .{.clear = false}) catch unreachable; | ||
# } | ||
#}; | ||
# | ||
#pub fn create_released(pid: beam.pid) PidResource { | ||
# const resource = PidResource.create(pid, .{}) catch unreachable; | ||
# return resource; | ||
#} | ||
# | ||
#pub fn keep(resource: PidResource) void { | ||
# resource.keep(); | ||
#} | ||
# | ||
#pub fn manual_keep(term: beam.term, should_keep: bool) void { | ||
# const resource = beam.get(PidResource, term, .{.keep = false}) catch unreachable; | ||
# if (should_keep) { | ||
# resource.keep(); | ||
# } | ||
#} | ||
#""" | ||
# | ||
#test "you can create then release an item" do | ||
# this = self() | ||
# | ||
# spawn(fn -> create_released(this) end) | ||
# | ||
# assert_receive :cleaned, 100 | ||
#end | ||
# | ||
#test "an unkept resource is never released" do | ||
# this = self() | ||
# | ||
# spawn(fn -> | ||
# this | ||
# |> create_released() | ||
# |> keep() | ||
# end) | ||
# | ||
# refute_receive :cleaned, 500 | ||
#end | ||
# | ||
#describe "you can manually use beam.get to" do | ||
# test "decide to not keep" do | ||
# this = self() | ||
# | ||
# spawn(fn -> | ||
# this | ||
# |> create_released() | ||
# |> manual_keep(false) | ||
# end) | ||
# | ||
# assert_receive :cleaned, 100 | ||
# end | ||
# | ||
# test "decide to keep" do | ||
# this = self() | ||
# | ||
# spawn(fn -> | ||
# this | ||
# |> create_released() | ||
# |> manual_keep(true) | ||
# end) | ||
# | ||
# refute_receive :cleaned, 500 | ||
# end | ||
#end | ||
use Zig, otp_app: :zigler, resources: [:PidResource] | ||
|
||
~Z""" | ||
const beam = @import("beam"); | ||
const std = @import("std"); | ||
const Resource = beam.Resource; | ||
pub const PidResource = Resource(beam.pid, @import("root"), .{.Callbacks = PidResourceCallbacks}); | ||
pub const PidResourceCallbacks = struct { | ||
pub fn dtor(pid: *beam.pid) void { | ||
_ = beam.send(pid.*, .cleaned, .{.clear = false}) catch unreachable; | ||
} | ||
}; | ||
pub fn create_released(pid: beam.pid) PidResource { | ||
const resource = PidResource.create(pid, .{}) catch unreachable; | ||
return resource; | ||
} | ||
pub fn keep(resource: PidResource) void { | ||
resource.keep(); | ||
} | ||
pub fn manual_keep(term: beam.term, should_keep: bool) void { | ||
const resource = beam.get(PidResource, term, .{.keep = false}) catch unreachable; | ||
if (should_keep) { | ||
resource.keep(); | ||
} | ||
} | ||
""" | ||
|
||
test "you can create then release an item" do | ||
this = self() | ||
|
||
spawn(fn -> create_released(this) end) | ||
|
||
assert_receive :cleaned, 100 | ||
end | ||
|
||
test "an unkept resource is never released" do | ||
this = self() | ||
|
||
spawn(fn -> | ||
this | ||
|> create_released() | ||
|> keep() | ||
end) | ||
|
||
refute_receive :cleaned, 500 | ||
end | ||
|
||
describe "you can manually use beam.get to" do | ||
test "decide to not keep" do | ||
this = self() | ||
|
||
spawn(fn -> | ||
this | ||
|> create_released() | ||
|> manual_keep(false) | ||
end) | ||
|
||
assert_receive :cleaned, 100 | ||
end | ||
|
||
test "decide to keep" do | ||
this = self() | ||
|
||
spawn(fn -> | ||
this | ||
|> create_released() | ||
|> manual_keep(true) | ||
end) | ||
|
||
refute_receive :cleaned, 500 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,67 @@ | ||
defmodule ZiglerTest.Resource.ReleaseTest do | ||
use ZiglerTest.IntegrationCase, async: true | ||
|
||
@moduletag :resource | ||
test "restore" | ||
|
||
# use Zig, otp_app: :zigler, resources: [:PidResource] | ||
# | ||
# ~Z""" | ||
# const beam = @import("beam"); | ||
# const std = @import("std"); | ||
# const Resource = beam.Resource; | ||
# | ||
# pub const PidResource = Resource(beam.pid, @import("root"), .{.Callbacks = PidResourceCallbacks}); | ||
# | ||
# pub const PidResourceCallbacks = struct { | ||
# pub fn dtor(pid: *beam.pid) void { | ||
# _ = beam.send(pid.*, .cleaned, .{.clear = false}) catch unreachable; | ||
# } | ||
# }; | ||
# | ||
# pub fn create_no_release(pid: beam.pid) PidResource { | ||
# const resource = PidResource.create(pid, .{.released = false}) catch unreachable; | ||
# return resource; | ||
# } | ||
# | ||
# pub fn create_released(pid: beam.pid) PidResource { | ||
# const resource = PidResource.create(pid, .{}) catch unreachable; | ||
# return resource; | ||
# } | ||
# | ||
# pub fn release(resource: PidResource) void { | ||
# resource.release(); | ||
# } | ||
# """ | ||
# | ||
# test "you can create a released item" do | ||
# this = self() | ||
# | ||
# spawn(fn -> | ||
# create_released(this) | ||
# end) | ||
# | ||
# assert_receive :cleaned, 100 | ||
# end | ||
# | ||
# # it's not entirely clear as to why this doesn't work: it should. | ||
# @tag :skip | ||
# test "you can create then release an item" do | ||
# this = self() | ||
# | ||
# spawn(fn -> | ||
# this | ||
# |> create_no_release() | ||
# |> release() | ||
# end) | ||
# | ||
# assert_receive :cleaned | ||
# end | ||
# | ||
# test "an unkept resource is never released" do | ||
# this = self() | ||
# | ||
# spawn(fn -> | ||
# create_no_release(this) | ||
# end) | ||
# | ||
# refute_receive :cleaned, 500 | ||
# end | ||
use ZiglerTest.IntegrationCase, async: true | ||
|
||
use Zig, otp_app: :zigler, resources: [:PidResource] | ||
|
||
~Z""" | ||
const beam = @import("beam"); | ||
const std = @import("std"); | ||
const Resource = beam.Resource; | ||
pub const PidResource = Resource(beam.pid, @import("root"), .{.Callbacks = PidResourceCallbacks}); | ||
pub const PidResourceCallbacks = struct { | ||
pub fn dtor(pid: *beam.pid) void { | ||
_ = beam.send(pid.*, .cleaned, .{.clear = false}) catch unreachable; | ||
} | ||
}; | ||
pub fn create_no_release(pid: beam.pid) PidResource { | ||
const resource = PidResource.create(pid, .{.released = false}) catch unreachable; | ||
return resource; | ||
} | ||
pub fn create_released(pid: beam.pid) PidResource { | ||
const resource = PidResource.create(pid, .{}) catch unreachable; | ||
return resource; | ||
} | ||
pub fn release(resource: PidResource) void { | ||
resource.release(); | ||
} | ||
""" | ||
|
||
test "you can create a released item" do | ||
this = self() | ||
|
||
spawn(fn -> | ||
create_released(this) | ||
end) | ||
|
||
assert_receive :cleaned, 100 | ||
end | ||
|
||
# it's not entirely clear as to why this doesn't work: it should. | ||
@tag :skip | ||
test "you can create then release an item" do | ||
this = self() | ||
|
||
spawn(fn -> | ||
this | ||
|> create_no_release() | ||
|> release() | ||
end) | ||
|
||
assert_receive :cleaned | ||
end | ||
|
||
test "an unkept resource is never released" do | ||
this = self() | ||
|
||
spawn(fn -> | ||
create_no_release(this) | ||
end) | ||
|
||
refute_receive :cleaned, 500 | ||
end | ||
end |