@@ -140,9 +140,32 @@ def test_close_is_idempotent(clients):
140140 client .consumer .close .assert_called_once ()
141141
142142
143- def test_init_conflict_cleans_up_new_client_without_stopping_owner_sampler (
143+ def test_init_with_other_dsn_warns_and_leaves_tracking_with_owner (
144+ clients , monkeypatch , caplog
145+ ):
146+ caplog .set_level ("WARNING" , logger = "wildedge" )
147+ first = clients ()
148+ route = claim_instrumentation (first , "onnx" )
149+ set_default_client (first )
150+ second = clients ()
151+ monkeypatch .setattr (convenience , "WildEdge" , lambda ** kwargs : second )
152+ with patch ("wildedge.client.stop_sampler" ) as stop :
153+ result = wildedge .init (
154+ dsn = "https://other@localhost/other" , integrations = ["onnx" ]
155+ )
156+ stop .assert_not_called ()
157+ assert result is second
158+ assert not second .closed
159+ assert not first .closed
160+ assert route () is first
161+ assert convenience .peek_default_client () is second
162+ assert [r for r in caplog .records if "already owns automatic tracking" in r .message ]
163+
164+
165+ def test_strict_ownership_conflict_raises_and_cleans_up_new_client (
144166 clients , monkeypatch
145167):
168+ instrumentation .set_strict_ownership (True )
146169 first = clients ()
147170 claim_instrumentation (first , "onnx" )
148171 set_default_client (first )
@@ -157,6 +180,50 @@ def test_init_conflict_cleans_up_new_client_without_stopping_owner_sampler(
157180 assert convenience .peek_default_client () is first
158181
159182
183+ def test_init_with_owner_dsn_reuses_owner (clients , monkeypatch , caplog ):
184+ caplog .set_level ("WARNING" , logger = "wildedge" )
185+ first = clients ()
186+ first .instrument ("onnx" )
187+ set_default_client (first )
188+ monkeypatch .setattr (
189+ convenience , "WildEdge" , MagicMock (side_effect = AssertionError ("new client" ))
190+ )
191+ assert (
192+ wildedge .init (dsn = "https://test@localhost/test" , integrations = ["onnx" ]) is first
193+ )
194+ assert not [r for r in caplog .records if "ignoring" in r .message ]
195+
196+
197+ def test_init_with_dsn_none_reuses_owner_of_env_dsn (clients , monkeypatch ):
198+ first = clients ()
199+ set_default_client (first )
200+ monkeypatch .setenv ("WILDEDGE_DSN" , "https://test@localhost/test" )
201+ monkeypatch .setattr (
202+ convenience , "WildEdge" , MagicMock (side_effect = AssertionError ("new client" ))
203+ )
204+ assert wildedge .init (dsn = None ) is first
205+
206+
207+ def test_hub_conflict_warns_and_skips (clients , monkeypatch , caplog ):
208+ caplog .set_level ("WARNING" , logger = "wildedge" )
209+ installs = []
210+
211+ class Tracker :
212+ def can_install (self ):
213+ return True
214+
215+ def install_patch (self , route ):
216+ installs .append (route )
217+
218+ monkeypatch .setitem (WildEdge .HUB_TRACKER_CLASSES , "huggingface" , Tracker )
219+ first , second = clients (), clients ()
220+ first .instrument (None , hubs = ["huggingface" ])
221+ second .instrument (None , hubs = ["huggingface" ])
222+ assert len (installs ) == 1
223+ assert "huggingface" not in second .hub_trackers
224+ assert [r for r in caplog .records if "already owns automatic tracking" in r .message ]
225+
226+
160227@pytest .mark .parametrize ("hub" , ["huggingface" , "torchhub" ])
161228def test_hub_factory_route_follows_replacement (clients , monkeypatch , hub ):
162229 routes = []
0 commit comments