Skip to content

Instantly share code, notes, and snippets.

@davisp
Created June 4, 2009 21:26
Show Gist options
  • Save davisp/123860 to your computer and use it in GitHub Desktop.
Save davisp/123860 to your computer and use it in GitHub Desktop.
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -pa ./src/couchdb -sasl errlog_type error -boot start_sasl -noshell
%%
main(_) ->
code:add_pathz("src/couchdb"),
application:load(crypto),
crypto:start(),
etap:plan(unknown),
case (catch test()) of
ok ->
etap:end_tests();
Other ->
etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
etap:bail(Other)
end,
ok.
%%
test() ->
%%
%% to_existing_atom
%%
etap:is(true, couch_util:to_existing_atom(true), "An atom is an atom."),
etap:is(foo, couch_util:to_existing_atom(<<"foo">>), "A binary foo is the atom foo."),
etap:is(foobarbaz, couch_util:to_existing_atom("foobarbaz"), "A list of atoms is one munged atom."),
%%
%% terminate_linked
%%
test_terminate(normal),
test_terminate(foo),
%%
%% new_uuid
%%
etap:isnt(couch_util:new_uuid(), couch_util:new_uuid(), "A guid ought to be unique."),
%%
%% implode
%%
etap:is([1, 38, 2, 38, 3], couch_util:implode([1,2,3],"&"), "use & as separator in list."),
%%
%% trim
%%
Strings = [" foo", "foo ", "\tfoo", " foo ", "foo\t", "foo\n", "\nfoo"],
etap:ok(lists:all(fun(S) -> couch_util:trim(S) == "foo" end, Strings),"everything here trimmed should be foo."),
%%
%% abs_pathname
%%
{ok, Cwd} = file:get_cwd(),
etap:is(Cwd ++ "/foo", couch_util:abs_pathname("./foo"), "foo is in this directory."),
%%
%% should_flush
%%
etap:ok(not couch_util:should_flush(), "memory is below 10000000 threshhold."),
%% increase memory of the process
%% IntsToAGazillion = lists:seq(1,2000000),
%% BigTab = ets:new(bighash, []),
%% lists:foreach(fun(Int) -> ets:insert(BigTab, {Int, <<"foobar">>}) end, IntsToAGazillion),
%% etap:ok(not couch_util:should_flush(), "should flush will invoke gc to reduce memory."),
%% cleanup
%%ets:delete(BigTab),
%%
Self = self(),
spawn(fun() ->
ChildPid = spawn_link(fun() -> receive shutdown -> ok end
end),
couch_util:terminate_linked(normal),
Self ! {pid, ChildPid}
end),
receive
{pid, Pid} ->
etap:ok(not is_process_alive(ChildPid), "why wont this work?")
after 1000
% make an error
end,
%%
%% done
%%
ok.
%%
%%
test_terminate(Msg) ->
spawn(test_util_helper, test_terminate_pid, [Msg, self()]),
Result = receive false -> true;
_ -> false end,
etap:ok(Result, "process ought to be dead.").
%%
%%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment