Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for flaky renameuser test using polling-until-expected-result #493

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions test/teiserver/protocols/spring/spring_auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ defmodule Teiserver.SpringAuthTest do
new_user: 2
]

import Teiserver.Support.Tachyon, only: [poll_until: 2]

setup do
%{socket: socket, user: user} = auth_setup()
{:ok, socket: socket, user: user}
Expand Down Expand Up @@ -442,9 +444,6 @@ CLIENTS test_room #{user.name}\n"
assert reply =~ "s.battles.id_list "
end

# this is a flakey test, but only when other tests are running around.
# you may be able to reproduce with the seed 846266
@tag :needs_attention
test "RENAMEACCOUNT", %{socket: socket, user: user} do
old_name = user.name
new_name = "test_user_rename"
Expand All @@ -461,12 +460,15 @@ CLIENTS test_room #{user.name}\n"
# Rename with an invalid name
_send_raw(socket, "RENAMEACCOUNT Y--Y\n")
reply = _recv_raw(socket)
assert reply == "SERVERMSG Invalid characters in name (only a-z, A-Z, 0-9, [, ] allowed)\n"
assert String.starts_with?(reply, "SERVERMSG")
assert String.contains?(reply, "Invalid characters in name")
assert String.contains?(reply, "(only a-z, A-Z, 0-9, [, ] allowed)")

# Rename with existng name
_send_raw(socket, "RENAMEACCOUNT #{watcher_user.name}\n")
reply = _recv_raw(socket)
assert reply == "SERVERMSG Username already taken\n"
assert String.starts_with?(reply, "SERVERMSG")
assert String.contains?(reply, "Username already taken")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine as it is. But you can leverage Elixir's pattern matching for something a little bit more direct:

assert "SERVERMSG" <> rest = _recv_raw(socket)
assert String.contains?(rest,)


# Perform rename
_send_raw(socket, "RENAMEACCOUNT test_user_rename\n")
Expand All @@ -478,10 +480,12 @@ CLIENTS test_room #{user.name}\n"
wreply = _recv_raw(watcher)
assert wreply == "REMOVEUSER #{user.name}\n"

# Give it a chance to perform some of the actions
:timer.sleep(250)
L-e-x-o-n marked this conversation as resolved.
Show resolved Hide resolved

assert Client.get_client_by_id(userid) == nil
Teiserver.Support.Tachyon.poll_until(
fn -> Client.get_client_by_id(userid) end,
&(&1 == nil),
limit: 32,
wait: 250
)

# Lets be REAL sure
client_ids = Client.list_client_ids()
Expand All @@ -497,15 +501,24 @@ CLIENTS test_room #{user.name}\n"
_ = _recv_raw(socket)

# Now we get flood protection after the rename
_send_raw(
socket,
"LOGIN #{new_name} X03MO1qnZdYdgyfeuILPmQ== 0 * LuaLobby Chobby\t1993717506 0d04a635e200f308\tb sp\n"
expected_server_response = "DENIED Flood protection - Please wait 20 seconds and try again"

Teiserver.Support.Tachyon.poll_until(
fn ->
_send_raw(
socket,
"LOGIN #{new_name} X03MO1qnZdYdgyfeuILPmQ== 0 * LuaLobby Chobby\t1993717506 0d04a635e200f308\tb sp\n"
)

reply = _recv_until(socket)
[accepted | _remainder] = String.split(reply, "\n")
accepted
end,
&(&1 == expected_server_response),
limit: 32,
wait: 250
)

reply = _recv_until(socket)
[accepted | _remainder] = String.split(reply, "\n")
assert accepted == "DENIED Flood protection - Please wait 20 seconds and try again"

# Un-flood them
CacheUser.set_flood_level(userid, 0)
# And re-verify them
Expand Down
Loading