Skip to content

Commit

Permalink
gets resource keep/release up and running
Browse files Browse the repository at this point in the history
  • Loading branch information
ityonemo committed May 10, 2024
1 parent 723c604 commit 1aa5399
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 148 deletions.
157 changes: 77 additions & 80 deletions test/integration/resource/keep_test.exs
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
133 changes: 65 additions & 68 deletions test/integration/resource/release_test.exs
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

0 comments on commit 1aa5399

Please sign in to comment.