Skip to content

Commit

Permalink
Fix support for JWT in arena_load_test
Browse files Browse the repository at this point in the history
  • Loading branch information
AminArria committed Jun 14, 2024
1 parent e1da4c4 commit 595068a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 76 deletions.
3 changes: 1 addition & 2 deletions apps/arena_load_test/lib/arena_load_test/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ defmodule ArenaLoadTest.Application do
children = [
ArenaLoadTest.SocketSupervisor,
ArenaLoadTest.LoadtestManager,
{Finch, name: ArenaLoadTest.Finch},
ArenaLoadTest.TokenFetcher
{Finch, name: ArenaLoadTest.Finch}
# Starts a worker by calling: ArenaLoadTest.Worker.start_link(arg)
# {ArenaLoadTest.Worker, arg}
]
Expand Down
13 changes: 8 additions & 5 deletions apps/arena_load_test/lib/arena_load_test/game_socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ defmodule ArenaLoadTest.GameSocketHandler do
alias ArenaLoadTest.Utils
use WebSockex, restart: :transient

def start_link({client_id, game_id}) do
ws_url = ws_url(client_id, game_id)
def start_link({client_id, user_token, game_id}) do
ws_url = ws_url(client_id, user_token, game_id)

WebSockex.start_link(
ws_url,
__MODULE__,
%{
client_id: client_id,
user_token: user_token,
game_id: game_id
}
)
Expand Down Expand Up @@ -109,15 +110,17 @@ defmodule ArenaLoadTest.GameSocketHandler do
])
end

defp ws_url(client_id, game_id) do
defp ws_url(client_id, user_token, game_id) do
query_params = "gateway_jwt=#{user_token}"

case System.get_env("TARGET_SERVER") do
nil ->
"ws://localhost:4000/play/#{game_id}/#{client_id}"
"ws://localhost:4000/play/#{game_id}/#{client_id}?#{query_params}"

target_server ->
# TODO Replace this for a SSL connection using erlang credentials.
# TODO https://github.com/lambdaclass/mirra_backend/issues/493
"ws://#{Utils.get_server_ip(target_server)}:4000/play/#{game_id}/#{client_id}"
"ws://#{Utils.get_server_ip(target_server)}:4000/play/#{game_id}/#{client_id}?#{query_params}"
end
end

Expand Down
55 changes: 35 additions & 20 deletions apps/arena_load_test/lib/arena_load_test/socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ defmodule ArenaLoadTest.SocketHandler do
alias ArenaLoadTest.Utils

def start_link(client_id) do
ws_url = ws_url(client_id)
user_token = create_user(client_id)
ws_url = ws_url(client_id, user_token)

WebSockex.start_link(
ws_url,
__MODULE__,
%{
client_id: client_id
client_id: client_id,
user_token: user_token
}
)
end
Expand All @@ -29,43 +31,44 @@ defmodule ArenaLoadTest.SocketHandler do
end

@impl true
def handle_frame({:binary, game_state}, state) do
game_id = Serialization.GameState.decode(game_state).game_id
def handle_frame({:binary, lobby_event}, state) do
case Serialization.LobbyEvent.decode(lobby_event) do
%{event: {:game, %{game_id: game_id}}} ->
case :ets.lookup(:clients, state.client_id) do
[{client_id, _}] ->
:ets.delete(:clients, client_id)

case :ets.lookup(:clients, state.client_id) do
[{client_id, _}] ->
:ets.delete(:clients, client_id)
[] ->
raise KeyError, message: "Client with ID #{state.client_id} doesn't exist."
end

[] ->
raise KeyError, message: "Client with ID #{state.client_id} doesn't exist."
end
{:ok, pid} = SocketSupervisor.add_new_player(state.client_id, state.user_token, game_id)

{:ok, pid} =
SocketSupervisor.add_new_player(
state.client_id,
game_id
)
true = :ets.insert(:players, {state.client_id, game_id})

true = :ets.insert(:players, {state.client_id, game_id})
Process.send(pid, :send_action, [])

Process.send(pid, :send_action, [])
_ ->
:nothing
end

{:ok, state}
end

# Private
defp ws_url(player_id) do
defp ws_url(player_id, user_token) do
character = get_random_active_character()
player_name = "Player_#{player_id}"
query_params = "gateway_jwt=#{user_token}"

case System.get_env("TARGET_SERVER") do
nil ->
"ws://localhost:4000/join/#{player_id}/#{character}/#{player_name}"
"ws://localhost:4000/join/#{player_id}/#{character}/#{player_name}?#{query_params}"

target_server ->
# TODO Replace this for a SSL connection using erlang credentials.
# TODO https://github.com/lambdaclass/mirra_backend/issues/493
"ws://#{Utils.get_server_ip(target_server)}:4000/join/#{player_id}/#{character}/#{player_name}"
"ws://#{Utils.get_server_ip(target_server)}:4000/join/#{player_id}/#{character}/#{player_name}?#{query_params}"
end
end

Expand All @@ -75,4 +78,16 @@ defmodule ArenaLoadTest.SocketHandler do
["muflus", "h4ck", "uma"]
|> Enum.random()
end

defp create_user(client_id) do
gateway_url = Application.get_env(:arena_load_test, :gateway_url)
payload = Jason.encode!(%{client_id: to_string(client_id)})

{:ok, %{status: 200, body: body}} =
Finch.build(:post, "#{gateway_url}/curse/users", [{"content-type", "application/json"}], payload)
|> Finch.request(ArenaLoadTest.Finch)

Jason.decode!(body)
|> Map.get("gateway_jwt")
end
end
4 changes: 2 additions & 2 deletions apps/arena_load_test/lib/arena_load_test/socket_supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ defmodule ArenaLoadTest.SocketSupervisor do
@doc """
Initializes a websocket that handles the client connection in-game.
"""
def add_new_player(client_id, game_id) do
def add_new_player(client_id, user_token, game_id) do
DynamicSupervisor.start_child(
__MODULE__,
{GameSocketHandler, {client_id, game_id}}
{GameSocketHandler, {client_id, user_token, game_id}}
)
end

Expand Down
47 changes: 0 additions & 47 deletions apps/arena_load_test/lib/arena_load_test/token_fetcher.ex

This file was deleted.

0 comments on commit 595068a

Please sign in to comment.