11defmodule Flagsmith.Client.Poller.Test do
22 use ExUnit.Case , async: false
33
4- import Mox , only: [ verify_on_exit!: 1 , expect: 3 , allow: 3 , stub_with: 2 ]
4+ import Mox
55 import Flagsmith.Test.Helpers , only: [ assert_request: 2 ]
66
77 alias Flagsmith.Engine.Test
@@ -11,7 +11,7 @@ defmodule Flagsmith.Client.Poller.Test do
1111 @ api_url Flagsmith.Configuration . default_url ( )
1212 @ api_paths Flagsmith.Configuration . api_paths ( )
1313
14- # setup Mox to verify any expectations
14+ # setup Mox to verify any expectations
1515 setup :verify_on_exit!
1616
1717 # we start the supervisor as a supervised process so it's shut down on every test
@@ -502,131 +502,4 @@ defmodule Flagsmith.Client.Poller.Test do
502502 } } = Flagsmith.Client . get_environment_flags ( new_config )
503503 end
504504 end
505-
506- describe "api call errors" do
507- setup do
508- config =
509- Flagsmith.Client . new (
510- enable_local_evaluation: true ,
511- environment_key: "test_key"
512- )
513-
514- [ config: config ]
515- end
516-
517- test "initializing with api error" , % { config: config } do
518- expect ( Tesla.Adapter.Mock , :call , fn tesla_env , _options ->
519- assert_request (
520- tesla_env ,
521- body: nil ,
522- query: [ ] ,
523- headers: [ { @ environment_header , "test_key" } ] ,
524- url: Path . join ( [ @ api_url , @ api_paths . environment ] ) <> "/" ,
525- method: :get
526- )
527-
528- { :error , :noop }
529- end )
530-
531- # here we start it through the dynamic supervisor instead of directly
532- # since we don't want the process exit to exit the test as well
533- { :ok , pid } = Flagsmith.Client.Poller.Supervisor . start_child ( config )
534-
535- allow ( Tesla.Adapter.Mock , self ( ) , pid )
536-
537- # we assert that we'll have an exit from the function since the call will fail
538- assert catch_exit ( Flagsmith.Client . get_environment ( config ) )
539- end
540-
541- test "refresh with errors retries in next cycle" , % { config: config } do
542- config = % { config | environment_refresh_interval_milliseconds: 3 }
543- env_response = Jason . decode! ( Test.Generators . json_env ( ) )
544-
545- # expectation for first call
546- expect ( Tesla.Adapter.Mock , :call , fn tesla_env , _options ->
547- assert_request (
548- tesla_env ,
549- body: nil ,
550- query: [ ] ,
551- headers: [ { @ environment_header , "test_key" } ] ,
552- url: Path . join ( [ @ api_url , @ api_paths . environment ] ) <> "/" ,
553- method: :get
554- )
555-
556- { :ok , % Tesla.Env { status: 200 , body: env_response } }
557- end )
558-
559- # expectation for refresh call 1
560- expect ( Tesla.Adapter.Mock , :call , fn tesla_env , _options ->
561- assert_request (
562- tesla_env ,
563- body: nil ,
564- query: [ ] ,
565- headers: [ { @ environment_header , "test_key" } ] ,
566- url: Path . join ( [ @ api_url , @ api_paths . environment ] ) <> "/" ,
567- method: :get
568- )
569-
570- { :error , :noop }
571- end )
572-
573- # expectation for refresh call 2
574- # similar to what we did previously to test the refreshes
575- expect ( Tesla.Adapter.Mock , :call , fn tesla_env , _options ->
576- assert_request (
577- tesla_env ,
578- body: nil ,
579- query: [ ] ,
580- headers: [ { @ environment_header , "test_key" } ] ,
581- url: Path . join ( [ @ api_url , @ api_paths . environment ] ) <> "/" ,
582- method: :get
583- )
584-
585- poller_pid = Flagsmith.Client.Poller . whereis ( "test_key" )
586- :ok = :gen_statem . call ( poller_pid , { :update_refresh_rate , 60_000 } )
587- new_env_response = % { env_response | "feature_states" => [ ] }
588-
589- { :ok , % Tesla.Env { status: 200 , body: new_env_response } }
590- end )
591-
592- { :ok , pid } = Flagsmith.Client.Poller.Supervisor . start_child ( config )
593-
594- allow ( Tesla.Adapter.Mock , self ( ) , pid )
595-
596- :erlang . trace ( pid , true , [ :procs ] )
597-
598- # we should now have 2 spawns calls the first fails, so another one afterwards
599- assert Enum . reduce_while ( 1 .. 100 , false , fn _ , acc ->
600- receive do
601- { :trace , ^ pid , :spawn , spawned_pid ,
602- { Flagsmith.Client.Poller , :get_environment , [ ^ pid , ^ config ] } } ->
603- allow ( Tesla.Adapter.Mock , self ( ) , spawned_pid )
604-
605- case acc do
606- true ->
607- { :halt , true }
608-
609- false ->
610- { :cont , true }
611- end
612-
613- _others ->
614- { :cont , false }
615- after
616- 200 ->
617- { :halt , false }
618- end
619- end )
620-
621- assert Flagsmith.Test.Helpers . wait_until (
622- fn ->
623- { :ok , % Schemas.Flags { flags: flags } } =
624- Flagsmith.Client . get_environment_flags ( config )
625-
626- flags == % { }
627- end ,
628- 15
629- )
630- end
631- end
632505end
0 commit comments