Skip to content
Permalink
Browse files
Just use ETap
PropEr is fun and all but its super hard to debug failing tests and its
really not buying me that much in this case because I'm not really using
its generators for anything. ETap is good enough here.
  • Loading branch information
davisp committed Dec 24, 2012
1 parent f64b413 commit d7f227f01946b6fe70cea4c88a1f6dde1a2aacee
Show file tree
Hide file tree
Showing 13 changed files with 857 additions and 333 deletions.
@@ -1,3 +1,3 @@
.eunit/
deps/
ebin/
test/*.beam
@@ -25,11 +25,15 @@ build: deps
$(REBAR) compile


eunit:
$(REBAR) eunit skip_deps=true
etap: test/etap.beam test/tutil.beam
prove test/*.t


check: build eunit
check: build etap


%.beam: %.erl
erlc -o test/ $<


.PHONY: all clean distclean deps build eunit check

This file was deleted.

@@ -98,7 +98,7 @@ remove(#ets_lru{objects=Objs, atimes=ATs}=LRU, Key) ->
ok.


hit(#ets_lru{objects=Objs, atimes=ATs}=LRU, Key) ->
hit(#ets_lru{objects=Objs, atimes=ATs}, Key) ->
case ets:match(Objs, #entry{key=Key, atime='$1', _='_'}) of
[[ATime]] ->
NewATime = erlang:now(),
@@ -114,12 +114,13 @@ hit(#ets_lru{objects=Objs, atimes=ATs}=LRU, Key) ->
expire(#ets_lru{lifetime=undefined}) ->
ok;
expire(#ets_lru{objects=Objs, atimes=ATs, lifetime=LT}=LRU) ->
Now = os:timestamp(),
LTMicro = LT * 1000,
case ets:first(ATs) of
'$end_of_table' ->
ok;
ATime ->
case timer:now_diff(erlang:now(), ATime) > LTMicro of
case timer:now_diff(Now, ATime) > LTMicro of
true ->
[{ATime, Key}] = ets:lookup(ATs, ATime),
true = ets:delete(ATs, ATime),
@@ -0,0 +1,72 @@
#! /usr/bin/env escript

-define(WITH_LRU(F), tutil:with_lru(fun(LRU) -> F(LRU) end)).

main([]) ->
code:add_pathz("test"),
code:add_pathz("ebin"),

tutil:run(12, fun() -> test() end).


test() ->
test_lifecycle(),
?WITH_LRU(test_insert_lookup),
?WITH_LRU(test_insert_overwrite),
?WITH_LRU(test_insert_remove),
?WITH_LRU(test_member),
?WITH_LRU(test_clear),

ok.


test_lifecycle() ->
Resp = ets_lru:create(?MODULE, []),
etap:fun_is(
fun({ok, _LRU}) -> true; (_) -> false end,
Resp,
"ets_lru:create/2 returned an LRU"
),
{ok, LRU} = Resp,
etap:is(ok, ets_lru:destroy(LRU), "Destroyed the LRU ok").


test_insert_lookup(LRU) ->
ok = ets_lru:insert(LRU, foo, bar),
Resp = ets_lru:lookup(LRU, foo),
etap:is(Resp, {ok, bar}, "Lookup returned the inserted value").


test_insert_overwrite(LRU) ->
ok = ets_lru:insert(LRU, foo, bar),
Resp1 = ets_lru:lookup(LRU, foo),
etap:is(Resp1, {ok, bar}, "Lookup returned the inserted value"),
ok = ets_lru:insert(LRU, foo, bam),
Resp2 = ets_lru:lookup(LRU, foo),
etap:is(Resp2, {ok, bam}, "Lookup returned the newly inserted value").


test_insert_remove(LRU) ->
ok = ets_lru:insert(LRU, foo, bar),
Resp1 = ets_lru:lookup(LRU, foo),
etap:is(Resp1, {ok, bar}, "Lookup returned the inserted value"),
ok = ets_lru:remove(LRU, foo),
Resp2 = ets_lru:lookup(LRU, foo),
etap:is(Resp2, not_found, "Lookup returned not_found for removed value").


test_member(LRU) ->
etap:is(false, ets_lru:member(LRU, foo), "Not yet a member: foo"),
ok = ets_lru:insert(LRU, foo, bar),
etap:is(true, ets_lru:member(LRU, foo), "Now a member: foo"),
ok = ets_lru:remove(LRU, foo),
etap:is(false, ets_lru:member(LRU, foo), "No longer a member: foo").


test_clear(LRU) ->
ok = ets_lru:insert(LRU, foo, bar),
Resp1 = ets_lru:lookup(LRU, foo),
etap:is(Resp1, {ok, bar}, "Lookup returned the inserted value"),
ok = ets_lru:clear(LRU),
Resp2 = ets_lru:lookup(LRU, foo),
etap:is(Resp2, not_found, "Lookup returned not_found after a clear").
@@ -0,0 +1,69 @@
#! /usr/bin/env escript

main([]) ->
code:add_pathz("test"),
code:add_pathz("ebin"),

tutil:run(unknown, fun() -> test() end).


test() ->
test_named_tables(),
test_max_objects(),
test_max_size(),
test_lifetime(),
test_bad_option(),

ok.


test_named_tables() ->
{ok, LRU} = ets_lru:create(foo, [named_tables]),
etap:is(ets:info(foo_objects, size), 0, "foo_objects table exists"),
etap:is(ets:info(foo_atimes, size), 0, "foo_atimes table exists"),
ok = ets_lru:destroy(LRU),
etap:isnt(catch ets:info(foo_objects, size), 0, "foo_objects is gone"),
etap:isnt(catch ets:info(foo_atimes, size), 0, "foo_atimes is gone"),
ok.


test_max_objects() ->
% See also: 03-limit-max-objects.t
test_good([{max_objects, 5}]),
test_good([{max_objects, 1}]),
test_good([{max_objects, 923928342098203942}]).


test_max_size() ->
% See also: 04-limit-max-size.t
test_good([{max_size, 1}]),
test_good([{max_size, 5}]),
test_good([{max_size, 23423409090923423942309423094}]).


test_lifetime() ->
% See also: 05-limit-lifetime.t
test_good([{lifetime, 1}]),
test_good([{lifetime, 5}]),
test_good([{lifetime, 1244209909182409328409283409238}]).


test_bad_option() ->
test_bad([{bingo, bango}]),
test_bad([12]),
test_bad([true]).


test_good(Options) ->
etap:fun_is(fun
({ok, LRU}) -> ets_lru:destroy(LRU), true;
(_) -> false
end, ets_lru:create(?MODULE, Options), "LRU created ok with options").


test_bad(Options) ->
etap:fun_is(fun
({invalid_option, _}) -> true;
({ok, LRU}) -> ets_lru:destroy(LRU), false;
(_) -> false
end, catch ets_lru:create(?MODULE, Options), "LRU error with options").
@@ -0,0 +1,26 @@
#! /usr/bin/env escript

objs() -> 25.

main([]) ->
code:add_pathz("test"),
code:add_pathz("ebin"),

tutil:run(1, fun() -> test() end).


test() ->
{ok, LRU} = ets_lru:create(lru, [named_tables, {max_objects, objs()}]),
etap:is(insert_kvs(LRU, 100 * objs()), ok, "Max object count ok"),
ok = ets_lru:destroy(LRU).


insert_kvs(LRU, 0) ->
ok;
insert_kvs(LRU, Count) ->
ets_lru:insert(LRU, Count, bar),
case ets:info(lru_objects, size) > objs() of
true -> erlang:error(exceeded_max_objects);
false -> ok
end,
insert_kvs(LRU, Count-1).
@@ -0,0 +1,26 @@
#! /usr/bin/env escript

max_size() -> 1024.

main([]) ->
code:add_pathz("test"),
code:add_pathz("ebin"),

tutil:run(1, fun() -> test() end).


test() ->
{ok, LRU} = ets_lru:create(lru, [named_tables, {max_size, max_size()}]),
etap:is(insert_kvs(LRU, 10000), ok, "Max size ok"),
ok = ets_lru:destroy(LRU).


insert_kvs(LRU, 0) ->
ok;
insert_kvs(LRU, Count) ->
ets_lru:insert(LRU, Count, 1.5234),
case ets:info(lru_objects, memory) > max_size() of
true -> erlang:error(exceeded_max_size);
false -> ok
end,
insert_kvs(LRU, Count-1).
@@ -0,0 +1,15 @@
#! /usr/bin/env escript

lifetime() -> 1024.

main([]) ->
code:add_pathz("test"),
code:add_pathz("ebin"),

tutil:run(unknown, fun() -> test() end).


test() ->
{ok, LRU} = ets_lru:create(lru, [named_tables, {lifetime, lifetime()}]),
% Figure out how to test this.
ok = ets_lru:destroy(LRU).

0 comments on commit d7f227f

Please sign in to comment.