Skip to content

Commit 889a54e

Browse files
NSHkrNSHkr
authored andcommitted
mix format
1 parent bc54d61 commit 889a54e

21 files changed

+595
-436
lines changed

lib/elixir_scope/foundation/CURSOR_OTP.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ The current approach treats GenServer processes as isolated units rather than de
142142
- [ ] State management through restarts, not manual reset
143143

144144
## Test Architecture
145-
- [ ] Test isolation through supervision
145+
- [ ] Test isolation through supervisaion
146146
- [ ] No manual process management in tests
147147
- [ ] Proper setup/teardown using OTP lifecycle
148148
- [ ] Concurrent test safety through process boundaries

lib/elixir_scope/foundation/application.ex

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ defmodule ElixirScope.Foundation.Application do
1414
children = [
1515
# Registry must start first for service discovery
1616
{ElixirScope.Foundation.ProcessRegistry, []},
17-
17+
1818
# Core foundation services with production namespace
19-
{ElixirScope.Foundation.Services.ConfigServer,
20-
[namespace: :production]},
21-
{ElixirScope.Foundation.Services.EventStore,
22-
[namespace: :production]},
23-
{ElixirScope.Foundation.Services.TelemetryService,
24-
[namespace: :production]},
19+
{ElixirScope.Foundation.Services.ConfigServer, [namespace: :production]},
20+
{ElixirScope.Foundation.Services.EventStore, [namespace: :production]},
21+
{ElixirScope.Foundation.Services.TelemetryService, [namespace: :production]},
2522

2623
# TestSupervisor for dynamic test isolation
2724
{ElixirScope.Foundation.TestSupervisor, []},

lib/elixir_scope/foundation/lib/elixir_scope/foundation/test_supervisor.ex

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ defmodule ElixirScope.Foundation.TestSupervisor do
6161
@spec start_isolated_services(test_ref()) :: {:ok, [pid()]} | {:error, term()}
6262
def start_isolated_services(test_ref) when is_reference(test_ref) do
6363
namespace = {:test, test_ref}
64-
64+
6565
Logger.debug("Starting isolated services for test namespace #{inspect(namespace)}")
6666

6767
# Define the services to start with their configurations
@@ -72,19 +72,28 @@ defmodule ElixirScope.Foundation.TestSupervisor do
7272
]
7373

7474
# Start each service and collect results
75-
results =
75+
results =
7676
Enum.map(service_specs, fn {module, opts} ->
7777
case DynamicSupervisor.start_child(__MODULE__, {module, opts}) do
7878
{:ok, pid} ->
79-
Logger.debug("Started #{module} for test namespace #{inspect(namespace)}: #{inspect(pid)}")
79+
Logger.debug(
80+
"Started #{module} for test namespace #{inspect(namespace)}: #{inspect(pid)}"
81+
)
82+
8083
{:ok, pid}
8184

8285
{:error, {:already_started, pid}} ->
83-
Logger.warning("Service #{module} already started for namespace #{inspect(namespace)}: #{inspect(pid)}")
86+
Logger.warning(
87+
"Service #{module} already started for namespace #{inspect(namespace)}: #{inspect(pid)}"
88+
)
89+
8490
{:ok, pid}
8591

8692
{:error, reason} = error ->
87-
Logger.error("Failed to start #{module} for namespace #{inspect(namespace)}: #{inspect(reason)}")
93+
Logger.error(
94+
"Failed to start #{module} for namespace #{inspect(namespace)}: #{inspect(reason)}"
95+
)
96+
8897
error
8998
end
9099
end)
@@ -93,14 +102,22 @@ defmodule ElixirScope.Foundation.TestSupervisor do
93102
case Enum.split_with(results, &match?({:ok, _}, &1)) do
94103
{successes, []} ->
95104
pids = Enum.map(successes, fn {:ok, pid} -> pid end)
96-
Logger.info("Successfully started #{length(pids)} services for test namespace #{inspect(namespace)}")
105+
106+
Logger.info(
107+
"Successfully started #{length(pids)} services for test namespace #{inspect(namespace)}"
108+
)
109+
97110
{:ok, pids}
98111

99112
{_successes, failures} ->
100113
# If any failed, cleanup what we started and return error
101114
cleanup_namespace(test_ref)
102115
first_error = List.first(failures)
103-
Logger.error("Failed to start isolated services for test namespace #{inspect(namespace)}: #{inspect(first_error)}")
116+
117+
Logger.error(
118+
"Failed to start isolated services for test namespace #{inspect(namespace)}: #{inspect(first_error)}"
119+
)
120+
104121
first_error
105122
end
106123
end
@@ -126,10 +143,10 @@ defmodule ElixirScope.Foundation.TestSupervisor do
126143
@spec cleanup_namespace(test_ref()) :: :ok
127144
def cleanup_namespace(test_ref) when is_reference(test_ref) do
128145
namespace = {:test, test_ref}
129-
146+
130147
# Only log cleanup debug for non-empty namespaces
131148
services = ProcessRegistry.get_all_services(namespace)
132-
149+
133150
if map_size(services) > 0 do
134151
# Terminate each service through the DynamicSupervisor
135152
Enum.each(services, fn {_service_name, pid} ->
@@ -148,7 +165,7 @@ defmodule ElixirScope.Foundation.TestSupervisor do
148165

149166
# Wait a moment for cleanup to complete
150167
Process.sleep(50)
151-
168+
152169
# Use ProcessRegistry cleanup as backup
153170
ProcessRegistry.cleanup_test_namespace(test_ref)
154171
end
@@ -181,10 +198,10 @@ defmodule ElixirScope.Foundation.TestSupervisor do
181198
def get_test_namespaces_info() do
182199
# Get all children of the DynamicSupervisor
183200
children = DynamicSupervisor.which_children(__MODULE__)
184-
201+
185202
# Get registry stats
186203
registry_stats = ProcessRegistry.stats()
187-
204+
188205
%{
189206
active_children: length(children),
190207
test_namespaces: registry_stats.test_namespaces,
@@ -213,12 +230,12 @@ defmodule ElixirScope.Foundation.TestSupervisor do
213230
@spec wait_for_services_ready(reference(), pos_integer()) :: :ok | {:error, :timeout}
214231
def wait_for_services_ready(test_ref, timeout \\ 5000) when is_reference(test_ref) do
215232
namespace = {:test, test_ref}
216-
233+
217234
Logger.debug("Waiting for services to be ready in namespace #{inspect(namespace)}")
218235

219236
# Only check for services we actually start (now including TelemetryService)
220237
services_to_check = [:config_server, :event_store, :telemetry_service]
221-
238+
222239
start_time = System.monotonic_time(:millisecond)
223240
wait_for_services_loop(namespace, services_to_check, timeout, start_time)
224241
end
@@ -242,7 +259,7 @@ defmodule ElixirScope.Foundation.TestSupervisor do
242259
def namespace_healthy?(test_ref) when is_reference(test_ref) do
243260
namespace = {:test, test_ref}
244261
expected_services = [:config_server, :event_store, :telemetry_service]
245-
262+
246263
Enum.all?(expected_services, fn service ->
247264
case ServiceRegistry.health_check(namespace, service) do
248265
{:ok, _pid} -> true
@@ -253,12 +270,16 @@ defmodule ElixirScope.Foundation.TestSupervisor do
253270

254271
## Private Functions
255272

256-
@spec wait_for_services_loop(namespace(), [atom()], pos_integer(), integer()) :: :ok | {:error, :timeout}
273+
@spec wait_for_services_loop(namespace(), [atom()], pos_integer(), integer()) ::
274+
:ok | {:error, :timeout}
257275
defp wait_for_services_loop(namespace, services, timeout, start_time) do
258276
elapsed = System.monotonic_time(:millisecond) - start_time
259-
277+
260278
if elapsed >= timeout do
261-
Logger.warning("Timeout waiting for services in namespace #{inspect(namespace)} after #{elapsed}ms")
279+
Logger.warning(
280+
"Timeout waiting for services in namespace #{inspect(namespace)} after #{elapsed}ms"
281+
)
282+
262283
{:error, :timeout}
263284
else
264285
case check_all_services_ready(namespace, services) do
@@ -286,4 +307,4 @@ defmodule ElixirScope.Foundation.TestSupervisor do
286307
end
287308
end)
288309
end
289-
end
310+
end

lib/elixir_scope/foundation/process_registry.ex

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ defmodule ElixirScope.Foundation.ProcessRegistry do
2424
"""
2525

2626
@type namespace :: :production | {:test, reference()}
27-
@type service_name ::
28-
:config_server |
29-
:event_store |
30-
:telemetry_service |
31-
:test_supervisor
27+
@type service_name ::
28+
:config_server
29+
| :event_store
30+
| :telemetry_service
31+
| :test_supervisor
3232

3333
@type registry_key :: {namespace(), service_name()}
3434

@@ -66,9 +66,10 @@ defmodule ElixirScope.Foundation.ProcessRegistry do
6666
@spec register(namespace(), service_name(), pid()) :: :ok | {:error, {:already_registered, pid()}}
6767
def register(namespace, service, pid) when is_pid(pid) do
6868
case Registry.register(__MODULE__, {namespace, service}, nil) do
69-
{:ok, _owner} ->
69+
{:ok, _owner} ->
7070
:ok
71-
{:error, {:already_registered, existing_pid}} ->
71+
72+
{:error, {:already_registered, existing_pid}} ->
7273
{:error, {:already_registered, existing_pid}}
7374
end
7475
end
@@ -262,30 +263,32 @@ defmodule ElixirScope.Foundation.ProcessRegistry do
262263
@spec cleanup_test_namespace(reference()) :: :ok
263264
def cleanup_test_namespace(test_ref) do
264265
namespace = {:test, test_ref}
265-
266+
266267
# Get all PIDs in this namespace
267-
pids = Registry.select(__MODULE__, [
268-
{{{namespace, :"$1"}, :"$2", :"$3"}, [], [:"$2"]}
269-
])
270-
268+
pids =
269+
Registry.select(__MODULE__, [
270+
{{{namespace, :"$1"}, :"$2", :"$3"}, [], [:"$2"]}
271+
])
272+
271273
# Terminate each process gracefully
272274
Enum.each(pids, fn pid ->
273275
if Process.alive?(pid) do
274276
# Try graceful shutdown first
275277
Process.exit(pid, :shutdown)
276-
278+
277279
# Wait a brief moment for graceful shutdown
278280
receive do
279-
after 100 -> :ok
281+
after
282+
100 -> :ok
280283
end
281-
284+
282285
# Force kill if still alive
283286
if Process.alive?(pid) do
284287
Process.exit(pid, :kill)
285288
end
286289
end
287290
end)
288-
291+
289292
:ok
290293
end
291294

@@ -312,14 +315,16 @@ defmodule ElixirScope.Foundation.ProcessRegistry do
312315
partitions: pos_integer()
313316
}
314317
def stats() do
315-
all_entries = Registry.select(__MODULE__, [
316-
{{{:"$1", :"$2"}, :"$3", :"$4"}, [], [:"$1"]}
317-
])
318-
319-
{production_count, test_namespaces} =
318+
all_entries =
319+
Registry.select(__MODULE__, [
320+
{{{:"$1", :"$2"}, :"$3", :"$4"}, [], [:"$1"]}
321+
])
322+
323+
{production_count, test_namespaces} =
320324
Enum.reduce(all_entries, {0, MapSet.new()}, fn
321325
:production, {prod_count, test_set} ->
322326
{prod_count + 1, test_set}
327+
323328
{:test, ref}, {prod_count, test_set} ->
324329
{prod_count, MapSet.put(test_set, ref)}
325330
end)
@@ -331,4 +336,4 @@ defmodule ElixirScope.Foundation.ProcessRegistry do
331336
partitions: System.schedulers_online()
332337
}
333338
end
334-
end
339+
end

0 commit comments

Comments
 (0)