Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added capacity to check if a server is in continuum

  • Loading branch information...
commit 8eacc888b603726b70c41e710d50505d7812e758 1 parent b5054f2
@ddossot ddossot authored committed
Showing with 100 additions and 56 deletions.
  1. +23 −3 src/erlmc.erl
  2. +77 −53 t/erlmc_t_001.t
View
26 src/erlmc.erl
@@ -27,7 +27,8 @@
-module(erlmc).
-export([start/0, start/1, start_link/0, start_link/1, init/2,
- add_server/3, remove_server/2, refresh_server/3, add_connection/2, remove_connection/2]).
+ add_server/3, remove_server/2, refresh_server/3, has_server/2,
+ add_connection/2, remove_connection/2]).
%% api callbacks
-export([get/1, get_many/1, add/2, add/3, set/2, set/3,
@@ -66,7 +67,16 @@ refresh_server(Host, Port, PoolSize) ->
remove_server(Host, Port) ->
erlang:send(?MODULE, {remove_server, Host, Port}),
ok.
-
+
+has_server(Host, Port) ->
+ erlang:send(?MODULE, {has_server, self(), Host, Port}),
+
+ receive
+ {has_server_result, B} when is_boolean(B) -> B
+ after
+ 5000 -> unknown
+ end.
+
add_connection(Host, Port) ->
erlang:send(?MODULE, {add_connection, Host, Port}),
ok.
@@ -208,6 +218,8 @@ loop() ->
{remove_server, Host, Port} ->
[(catch gen_server:call(Pid, quit, ?TIMEOUT)) || [Pid] <- ets:match(erlmc_connections, {{Host, Port}, '$1'})],
remove_server_from_continuum(Host, Port);
+ {has_server, CallerPid, Host, Port} ->
+ CallerPid ! {has_server_result, is_server_in_continuum(Host, Port)};
{add_connection, Host, Port} ->
start_connection(Host, Port);
{remove_connection, Host, Port} ->
@@ -250,7 +262,15 @@ remove_server_from_continuum(Host, Port) ->
List ->
[ets:delete(erlmc_continuum, Key) || [Key] <- List]
end.
-
+
+is_server_in_continuum(Host, Port) ->
+ case ets:match(erlmc_continuum, {'$1', {Host, Port}}) of
+ [] ->
+ false;
+ _ ->
+ true
+ end.
+
package_key(Key) when is_atom(Key) ->
atom_to_list(Key);
View
130 t/erlmc_t_001.t
@@ -1,53 +1,77 @@
-#!/usr/bin/env escript
-%% -*- erlang -*-
-%%! -pa ./ebin -sasl errlog_type error -boot start_sasl -noshell
-
--include_lib("etap/include/etap.hrl").
-
-main(_) ->
- etap:plan(unknown),
-
- (fun() ->
- {ok, Socket} = gen_tcp:connect("localhost", 11211, [binary, {packet, 0}, {active, false}]),
-
- gen_tcp:send(Socket, <<128,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>),
- etap:ok((fun({ok, _}) -> true; (_) -> false end)(gen_tcp:recv(Socket, 0, 2000)), "noop"),
-
- gen_tcp:send(Socket, <<128,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>),
-
- ok
- end)(),
-
- (fun() ->
- etap:is(erlmc:start(), ok, "erlmc connect to default memcached server ok"),
-
- etap:is(erlmc:set("Hello", <<"World">>), <<>>, "set ok"),
- etap:is(erlmc:add("Hello", <<"Fail">>), <<"Data exists for key.">>, "add ok"),
- etap:is(erlmc:get("Hello"), <<"World">>, "get ok"),
- etap:is(erlmc:delete("Hello"), <<>>, "delete ok"),
- etap:is(erlmc:add("Hello", <<"World2">>), <<>>, "add ok"),
- etap:is(erlmc:get("Hello"), <<"World2">>, "get ok"),
- etap:is(erlmc:append("Hello", <<"!!!">>), <<>>, "append ok"),
- etap:is(erlmc:get("Hello"), <<"World2!!!">>, "get ok"),
- etap:is(erlmc:prepend("Hello", <<"$$$">>), <<>>, "prepend ok"),
- etap:is(erlmc:get("Hello"), <<"$$$World2!!!">>, "get ok"),
- etap:is(erlmc:delete("Hello"), <<>>, "delete ok"),
- etap:is(erlmc:get("Hello"), <<>>, "get ok"),
-
- erlmc:set("One", <<"A">>),
- erlmc:set("Two", <<"B">>),
- erlmc:set("Three", <<"C">>),
-
- etap:is(erlmc:get_many(["One", "Two", "Two-and-a-half", "Three"]), [{"One",<<"A">>},{"Two",<<"B">>},{"Two-and-a-half",<<>>},{"Three",<<"C">>}], "get_many ok"),
-
- etap:is(erlmc:flush(0), [{{"localhost",11211},<<>>}], "flush ok"),
-
- ?etap_match(erlmc:stats(), [{{"localhost",11211}, [{_,_}|_]}], "stats/0 ok"),
- ?etap_match(erlmc:stats("localhost",11211), [{_,_}|_], "stats/2 ok"),
-
- etap:is(erlmc:quit(), [{{"localhost",11211},[true]}], "quit ok"),
-
- ok
- end)(),
-
- etap:end_tests().
+Script started on Thu 10 Dec 2009 11:25:27 AM PST
+]0;ddossot@phrog: ~/erlang/dev/erlmc-ddossotddossot@phrog:~/erlang/dev/erlmc-ddossot$ make test
+sh ebin/erlmc.app.in 0.2
+mkdir -p ebin/
+(cd src;make)
+make[1]: Entering directory `/home/ddossot/erlang/dev/erlmc-ddossot/src'
+make[1]: Nothing to be done for `all'.
+make[1]: Leaving directory `/home/ddossot/erlang/dev/erlmc-ddossot/src'
+prove t/*.t
+t/erlmc_t_001.t ..
+
+t/erlmc_t_001.t .. No subtests run 
+
+Test Summary Report
+-------------------
+t/erlmc_t_001.t (Wstat: 0 Tests: 0 Failed: 0)
+ Parse errors: No plan found in TAP output
+Files=1, Tests=0, 0 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU)
+Result: FAIL
+make: *** [test] Error 1
+]0;ddossot@phrog: ~/erlang/dev/erlmc-ddossotddossot@phrog:~/erlang/dev/erlmc-ddossot$ make test; erl -pa ebintestmake test; erl -pa ebinhgllcd /home/ddossot/erlang/dev/erlmc-ddossot/llhg ebin[12@make ; erl -patestmake test; erl -pa ebinhgllcd /home/ddossot/erlang/dev/erlmc-ddossot/llhg ebin[12@make ; erl -patestmake ; script ./t/erlmc_t_001.t
+sh ebin/erlmc.app.in 0.2
+mkdir -p ebin/
+(cd src;make)
+make[1]: Entering directory `/home/ddossot/erlang/dev/erlmc-ddossot/src'
+make[1]: Nothing to be done for `all'.
+make[1]: Leaving directory `/home/ddossot/erlang/dev/erlmc-ddossot/src'
+Script started, file is ./t/erlmc_t_001.t
+]0;ddossot@phrog: ~/erlang/dev/erlmc-ddossotddossot@phrog:~/erlang/dev/erlmc-ddossot$ make ; erl -pa ebin
+]0;ddossot@phrog: ~/erlang/dev/erlmc-ddossotddossot@phrog:~/erlang/dev/erlmc-ddossot$
+]0;ddossot@phrog: ~/erlang/dev/erlmc-ddossotddossot@phrog:~/erlang/dev/erlmc-ddossot$ escript ./t/erlmc_t_001.t
+escript: Premature end of file reached
+]0;ddossot@phrog: ~/erlang/dev/erlmc-ddossotddossot@phrog:~/erlang/dev/erlmc-ddossot$ escript ./t/erlmc_t_001.t
+# Current time local 2009-12-10 11:26:25
+# Using etap version "0.3.4"
+ok 1 - noop
+ok 2 - erlmc connect to default memcached server ok
+ok 3 - set ok
+ok 4 - add ok
+ok 5 - get ok
+ok 6 - delete ok
+ok 7 - add ok
+ok 8 - get ok
+ok 9 - append ok
+ok 10 - get ok
+ok 11 - prepend ok
+ok 12 - get ok
+ok 13 - delete ok
+ok 14 - get ok
+ok 15 - get_many ok
+ok 16 - flush ok
+ok 17 - stats/0 ok
+ok 18 - stats/2 ok
+ok 19 - quit ok
+ok 20 - has_server ok
+ok 21 - remove ok
+ok 22 - second has_server ok
+1..22
+]0;ddossot@phrog: ~/erlang/dev/erlmc-ddossotddossot@phrog:~/erlang/dev/erlmc-ddossot$ rake test
+rake aborted!
+No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
+/usr/lib/ruby/1.8/rake.rb:2143:in `raw_load_rakefile'
+(See full trace by running task with --trace)
+]0;ddossot@phrog: ~/erlang/dev/erlmc-ddossotddossot@phrog:~/erlang/dev/erlmc-ddossot$ maket test
+sh ebin/erlmc.app.in 0.2
+mkdir -p ebin/
+(cd src;make)
+make[1]: Entering directory `/home/ddossot/erlang/dev/erlmc-ddossot/src'
+make[1]: Nothing to be done for `all'.
+make[1]: Leaving directory `/home/ddossot/erlang/dev/erlmc-ddossot/src'
+prove t/*.t
+t/erlmc_t_001.t ..
+t/erlmc_t_001.t .. 1/?
+
+t/erlmc_t_001.t .. ok
+All tests successful.
+Files=1, Tests=22, 0 wallclock secs ( 0.04 usr 0.00 sys + 0.14 cusr 0.02 csys = 0.20 CPU)
Please sign in to comment.
Something went wrong with that request. Please try again.