Tests: Add a helper method to reduce sleeping times in test.
authorlain <lain@soykaf.club>
Wed, 16 Dec 2020 09:39:36 +0000 (10:39 +0100)
committerlain <lain@soykaf.club>
Mon, 21 Dec 2020 15:31:23 +0000 (16:31 +0100)
This will 'time travel', i.e. change the inserted_at and update_at
fields of the object in question. This is used to backdate things
were we used sleeping before to ensure time differences.

test/pleroma/chat_test.exs
test/pleroma/conversation/participation_test.exs
test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs
test/pleroma/web/twitter_api/password_controller_test.exs
test/support/helpers.ex

index 9e8a9ebf01abcca2ba80528c72ecef060e79afc8..1dd04916c1b1219ff6d0e5f7225bde2cebe0837c 100644 (file)
@@ -73,7 +73,8 @@ defmodule Pleroma.ChatTest do
       other_user = insert(:user)
 
       {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
-      :timer.sleep(1500)
+      {:ok, chat} = time_travel(chat, -2)
+
       {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
 
       assert chat.id == chat_two.id
index e72c49b29529a9a58ab373eb9be3bf2b81917b78..122b10486f8562ff00f491efbfb8fc22d6feb6d9 100644 (file)
@@ -96,12 +96,11 @@ defmodule Pleroma.Conversation.ParticipationTest do
     {:ok, %Participation{} = participation} =
       Participation.create_for_user_and_conversation(user, conversation)
 
+    {:ok, participation} = time_travel(participation, -2)
+
     assert participation.user_id == user.id
     assert participation.conversation_id == conversation.id
 
-    # Needed because updated_at is accurate down to a second
-    :timer.sleep(1000)
-
     # Creating again returns the same participation
     {:ok, %Participation{} = participation_two} =
       Participation.create_for_user_and_conversation(user, conversation)
index a6c9d0c1b56bb26a247ff3963f5c59df72bb5ca4..415c3decdbca7ffb24d54fdf0648c94182fd29fb 100644 (file)
@@ -394,11 +394,11 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
       tridi = insert(:user)
 
       {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
-      :timer.sleep(1000)
-      {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
-      :timer.sleep(1000)
+      {:ok, chat_1} = time_travel(chat_1, -3)
+      {:ok, chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
+      {:ok, _chat_2} = time_travel(chat_2, -2)
       {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
-      :timer.sleep(1000)
+      {:ok, chat_3} = time_travel(chat_3, -1)
 
       # bump the second one
       {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
index 6d08075cc5187e4b36eae23ab1d4f3237f11a2ac..c1f5bc5c7f08f37a5d23e29d2dad9a60360c1b1f 100644 (file)
@@ -37,8 +37,7 @@ defmodule Pleroma.Web.TwitterAPI.PasswordControllerTest do
 
       user = insert(:user)
       {:ok, token} = PasswordResetToken.create_token(user)
-
-      :timer.sleep(2000)
+      {:ok, token} = time_travel(token, -2)
 
       response =
         conn
@@ -55,7 +54,7 @@ defmodule Pleroma.Web.TwitterAPI.PasswordControllerTest do
 
       user = insert(:user)
       {:ok, token} = PasswordResetToken.create_token(user)
-      :timer.sleep(2000)
+      {:ok, token} = time_travel(token, -2)
       {:ok, _access_token} = Token.create(insert(:oauth_app), user, %{})
 
       params = %{
index 224034521e683e135f7a354711f43aa23352109d..15e8cbd9d51aeee9cdb289d3e9e012566daa55dd 100644 (file)
@@ -55,6 +55,14 @@ defmodule Pleroma.Tests.Helpers do
           clear_config: 2
         ]
 
+      def time_travel(entity, seconds) do
+        new_time = NaiveDateTime.add(entity.inserted_at, seconds)
+
+        entity
+        |> Ecto.Changeset.change(%{inserted_at: new_time, updated_at: new_time})
+        |> Pleroma.Repo.update()
+      end
+
       def to_datetime(%NaiveDateTime{} = naive_datetime) do
         naive_datetime
         |> DateTime.from_naive!("Etc/UTC")