diff --git a/test/integration/resource/keep_test.exs b/test/integration/resource/keep_test.exs index 5f3d1e61..af7fe4ff 100644 --- a/test/integration/resource/keep_test.exs +++ b/test/integration/resource/keep_test.exs @@ -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 diff --git a/test/integration/resource/release_test.exs b/test/integration/resource/release_test.exs index 65930fd6..e913d6a0 100644 --- a/test/integration/resource/release_test.exs +++ b/test/integration/resource/release_test.exs @@ -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