Skip to content

Commit

Permalink
shortly: fix final test, use pg instead of pg2
Browse files Browse the repository at this point in the history
  • Loading branch information
fkrause98 committed Sep 6, 2024
1 parent c81de39 commit b9ab650
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
10 changes: 5 additions & 5 deletions libraries/shortly/solution/shortly_notification_pg2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
unsubscribe/1,
notify/1]).

-define (PG2_NAME, shortly_notification_pg2_name).
-define (PG_NAME, shortly_notification_pg2_name).

init() ->
pg2:create(?PG2_NAME).
pg:create(?PG_NAME).

subscribe(Pid) ->
pg2:join(?PG2_NAME, Pid).
pg:join(?PG_NAME, Pid).

unsubscribe(Pid) ->
pg2:leave(?PG2_NAME, Pid).
pg:leave(?PG_NAME, Pid).

notify(Msg) ->
Subs = pg2:get_members(?PG2_NAME),
Subs = pg:get_members(?PG_NAME),
lists:foreach(
fun(Pid) ->
Pid ! Msg
Expand Down
2 changes: 1 addition & 1 deletion libraries/shortly/solution/shortly_syn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ init() ->
ok = syn:add_node_to_scopes([?SYN_NAME]).

subscribe(Pid) ->
syn:join(?SYN_NAME, "shortly-gruop", Pid).
ok = syn:join(?SYN_NAME, "shortly-group", Pid).

unsubscribe(Pid) ->
syn:leave(?SYN_NAME, "shortly-group", Pid).
Expand Down
4 changes: 2 additions & 2 deletions libraries/shortly/solution/shortly_ws_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ init(Req, Opts) ->
{cowboy_websocket,Req,Opts}.

websocket_init(State) ->
shortly_shortener:subscribe(self()),
ok = shortly_shortener:subscribe(self()),
{ok, State}.

websocket_handle(_Msg, State) ->
websocket_handle(Msg, State) ->
{ok, State}.

websocket_info(#{long_url := LongUrl, short_url := ShortUrl}, State) ->
Expand Down
24 changes: 11 additions & 13 deletions libraries/shortly/test/shortly_shortener_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ all() ->
test_created,
test_ok,
test_redirect,
%% test_ws,
test_ws,
run_eunit
].

Expand Down Expand Up @@ -103,30 +103,28 @@ do_get_request(Url) ->
{StatusCode, Headers, json_to_map(Body)}.

url(Url) ->
StringUrl = http_uri:encode(Url),
StringUrl = uri_string:quote(Url),
list_to_binary(StringUrl).

ws_connect(Path) ->
{ok, Pid} = gun:open("127.0.0.1", 8080, #{retry=>0}),
{ok, http} = gun:await_up(Pid),
Ref = monitor(process, Pid),
gun:ws_upgrade(Pid, Path, [], #{compress => true}),
receive
{gun_ws_upgrade, Pid, ok, _} ->
ok;
_ ->
error(failed)
end,
{Pid, Ref}.
{gun_upgrade, Pid, StreamRef, [<<"websocket">>], _} ->
{Pid, StreamRef};
Err ->
error(Err)
end.

ws_get({Pid,_}) ->
receive
{gun_ws, Pid, {text, Text}} ->
{gun_ws, Pid, _StreamRef, {text, Text}} ->
Text;
_ ->
error(failed)
Failed ->
error(Failed)
after 5000 ->
error(timout)
error(timeout)
end.

ws_terminate({Pid, Ref}) ->
Expand Down

0 comments on commit b9ab650

Please sign in to comment.