Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix some typo/thinko bugs and add a happy-path eunit test.

  • Loading branch information...
commit da053565f4b2cd1e9118f4a3eb0fdf8bee864143 1 parent fc3ea8c
@seancribbs seancribbs authored
View
4 src/riak_api_pb_listener.erl
@@ -86,8 +86,8 @@ code_change(_OldVsn, State, _Extra) -> {ok, State}.
%% when a new socket is accepted.
-spec new_connection(gen_tcp:socket(), #state{}) -> {ok, #state{}}.
new_connection(Socket, State) ->
- {ok, Pid} = riak_api_pb_socket_sup:start_socket(),
+ {ok, Pid} = riak_api_pb_sup:start_socket(),
ok = gen_tcp:controlling_process(Socket, Pid),
- ok = riak_api_pb_socket:set_socket(Pid, Socket),
+ ok = riak_api_pb_server:set_socket(Pid, Socket),
{ok, State}.
View
16 src/riak_api_pb_server.erl
@@ -160,19 +160,19 @@ code_change(_OldVsn,State,_Extra) ->
-spec process_message(atom(), term(), term(), #state{}) -> #state{}.
process_message(Service, Message, ServiceState, #state{states=ServiceStates}=ServerState) ->
case Service:process(Message, ServiceState) of
- %% Normal reply
- {reply, {ReplyCode, ReplyMessage}, NewServiceState} when is_integer(ReplyCode) ->
- {ok, Encoded} = Service:encode(ReplyCode, ReplyMessage),
- send_message(Encoded, ServerState),
- NewServiceStates = dict:update(Service, NewServiceState, ServiceStates),
- ServerState#state{states=NewServiceStates};
%% Streaming reply with reference
{reply, {stream, Ref}, NewServiceState} ->
- NewServiceStates = dict:update(Service, NewServiceState, ServiceStates),
+ NewServiceStates = dict:store(Service, NewServiceState, ServiceStates),
ServerState#state{states=NewServiceStates, req={Service,Ref}};
+ %% Normal reply
+ {reply, ReplyMessage, NewServiceState} ->
+ {ok, Encoded} = Service:encode(ReplyMessage),
+ send_message(Encoded, ServerState),
+ NewServiceStates = dict:store(Service, NewServiceState, ServiceStates),
+ ServerState#state{states=NewServiceStates};
{error, Message, NewServiceState} ->
send_error(Message, ServerState),
- NewServiceStates = dict:update(Service, NewServiceState, ServiceStates),
+ NewServiceStates = dict:store(Service, NewServiceState, ServiceStates),
ServerState#state{states=NewServiceStates};
Other ->
send_error("Unknown service response: ~p", [Other], ServerState),
View
73 test/pb_service_test.erl
@@ -0,0 +1,73 @@
+-module(pb_service_test).
+-compile([export_all]).
+-include_lib("eunit/include/eunit.hrl").
+
+%% ===================================================================
+%% Implement a dumb PB service
+%% ===================================================================
+-behaviour(riak_api_pb_service).
+-export([init/0,
+ decode/2,
+ encode/1,
+ process/2]).
+
+init() ->
+ undefined.
+
+decode(1, <<>>) ->
+ {ok, dummyreq};
+decode(_,_) ->
+ {error, unknown_message}.
+
+encode(ok) ->
+ {ok, <<2,$o,$k>>};
+encode(_) ->
+ error.
+
+process(dummyreq, State) ->
+ {reply, ok, State}.
+
+%% ===================================================================
+%% Eunit tests
+%% ===================================================================
+setup() ->
+ OldServices = riak_api_pb_service:dispatch_table(),
+ OldHost = app_helper:get_env(riak_api, pb_ip, "127.0.0.1"),
+ OldPort = app_helper:get_env(riak_api, pb_port, 8087),
+ application:set_env(riak_api, services, dict:new()),
+ application:set_env(riak_api, pb_ip, "127.0.0.1"),
+ application:set_env(riak_api, pb_port, 32767),
+ riak_api_pb_service:register(?MODULE, 1, 2),
+
+ application:load(sasl),
+ application:set_env(sasl, sasl_error_logger, {file, "pb_service_test_sasl.log"}),
+ error_logger:tty(false),
+ error_logger:logfile({open, "pb_service_test.log"}),
+ application:start(sasl),
+
+ ok = application:start(riak_api),
+ {OldServices, OldHost, OldPort}.
+
+cleanup({S, H, P}) ->
+ application:stop(riak_api),
+ application:set_env(riak_api, services, S),
+ application:set_env(riak_api, pb_ip, H),
+ application:set_env(riak_api, pb_port, P),
+ ok.
+
+request(Code, Payload) when is_binary(Payload), is_integer(Code) ->
+ Host = app_helper:get_env(riak_api, pb_ip),
+ Port = app_helper:get_env(riak_api, pb_port),
+ {ok, Socket} = gen_tcp:connect(Host, Port, [binary, {active, false}, {packet,4},
+ {header, 1}, {nodelay, true}]),
+ ok = gen_tcp:send(Socket, <<Code:8, Payload/binary>>),
+ {ok, Response} = gen_tcp:recv(Socket, 0),
+ Response.
+
+simple_test_() ->
+ {setup,
+ fun setup/0,
+ fun cleanup/1,
+ [
+ ?_assertEqual([2|<<"ok">>], request(1, <<>>))
+ ]}.
Please sign in to comment.
Something went wrong with that request. Please try again.