Skip to content

Commit 96635ea

Browse files
committed
Add test for unloading the NIF
Since there was an issue unloading the NIF, it would be good to have a unit test to verify that it doesn't appear again. This particular test would not have caught the issue, but it was useful when debugging.
1 parent b394493 commit 96635ea

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

test/circuits_gpio_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,30 @@ defmodule Circuits.GPIOTest do
242242
assert GPIO.read(gpio0) == 0
243243
GPIO.close(gpio0)
244244
end
245+
246+
test "unloading NIF" do
247+
# The theory here is that there shouldn't be a crash if this is reloaded a
248+
# few times.
249+
for _times <- 1..10 do
250+
assert {:module, Circuits.GPIO} == :code.ensure_loaded(Circuits.GPIO)
251+
assert {:module, Circuits.GPIO.Nif} == :code.ensure_loaded(Circuits.GPIO.Nif)
252+
253+
# Try running something to verify that it works.
254+
{:ok, gpio} = GPIO.open(1, :input)
255+
assert is_reference(gpio)
256+
257+
assert GPIO.info().pins_open == 1
258+
259+
GPIO.close(gpio)
260+
assert GPIO.info().pins_open == 0
261+
262+
assert true == :code.delete(Circuits.GPIO.Nif)
263+
assert true == :code.delete(Circuits.GPIO)
264+
265+
# The purge will call the unload which can be verified by turning DEBUG on
266+
# in the C code.
267+
assert false == :code.purge(Circuits.GPIO.Nif)
268+
assert false == :code.purge(Circuits.GPIO)
269+
end
270+
end
245271
end

0 commit comments

Comments
 (0)