@@ -169,4 +169,95 @@ defmodule Sentry.Test.ConfigIsolationTest do
169169 assert :ok = Sentry.Test.Config . allow ( self ( ) , nil )
170170 end
171171 end
172+
173+ describe "config scope isolation across an owner process's lifetime" do
174+ test "a worker stops resolving an owner's config once that owner exits" do
175+ parent = self ( )
176+ worker = spawn_env_probe ( )
177+ on_exit ( fn -> Process . exit ( worker , :kill ) end )
178+
179+ owner =
180+ spawn ( fn ->
181+ Sentry.Test.Config . put ( environment_name: "owner_a_env" )
182+ :ok = Sentry.Test.Config . allow ( self ( ) , worker )
183+ send ( parent , :allowed )
184+
185+ receive do
186+ :exit -> :ok
187+ end
188+ end )
189+
190+ assert_receive :allowed , 1_000
191+
192+ assert env_of ( worker ) == "owner_a_env"
193+
194+ ref = Process . monitor ( owner )
195+ send ( owner , :exit )
196+ assert_receive { :DOWN , ^ ref , :process , ^ owner , _ } , 1_000
197+
198+ assert wait_until ( fn -> env_of ( worker ) != "owner_a_env" end )
199+ end
200+
201+ test "a worker freed by a finished owner can be reclaimed and rebound by a new owner" do
202+ parent = self ( )
203+ worker = spawn_env_probe ( )
204+ on_exit ( fn -> Process . exit ( worker , :kill ) end )
205+
206+ first_owner =
207+ spawn ( fn ->
208+ Sentry.Test.Config . put ( environment_name: "first_owner_env" )
209+ :ok = Sentry.Test.Config . allow ( self ( ) , worker )
210+ send ( parent , :claimed )
211+
212+ receive do
213+ :exit -> :ok
214+ end
215+ end )
216+
217+ assert_receive :claimed , 1_000
218+ assert env_of ( worker ) == "first_owner_env"
219+
220+ assert_raise Scope.AllowConflictError , fn ->
221+ Sentry.Test.Config . allow ( self ( ) , worker )
222+ end
223+
224+ ref = Process . monitor ( first_owner )
225+ send ( first_owner , :exit )
226+ assert_receive { :DOWN , ^ ref , :process , ^ first_owner , _ } , 1_000
227+
228+ put_test_config ( environment_name: "second_owner_env" )
229+
230+ assert wait_until ( fn ->
231+ try do
232+ Sentry.Test.Config . allow ( self ( ) , worker ) == :ok
233+ rescue
234+ Scope.AllowConflictError -> false
235+ end
236+ end )
237+
238+ assert env_of ( worker ) == "second_owner_env"
239+ end
240+ end
241+
242+ defp spawn_env_probe do
243+ spawn ( fn -> env_probe_loop ( ) end )
244+ end
245+
246+ defp env_probe_loop do
247+ receive do
248+ { :env? , reply_to } ->
249+ send ( reply_to , { :env , Sentry.Config . environment_name ( ) } )
250+ env_probe_loop ( )
251+ end
252+ end
253+
254+ defp env_of ( probe ) do
255+ send ( probe , { :env? , self ( ) } )
256+
257+ receive do
258+ { :env , env } -> env
259+ after
260+ 100 -> :no_reply
261+ end
262+ end
172263end
0 commit comments