@@ -4396,14 +4396,44 @@ defmodule Kernel do
4396
4396
4397
4397
In order to speed up dispatching in production environments, where
4398
4398
all implementations are known up-front, Elixir provides a feature
4399
- called protocol consolidation. For this reason, all protocols are
4400
- compiled with `debug_info` set to `true`, regardless of the option
4401
- set by `elixirc` compiler. The debug info though may be removed after
4402
- consolidation.
4403
-
4404
- Protocol consolidation is applied by default to all Mix projects.
4405
- For applying consolidation manually, please check the functions in
4406
- the `Protocol` module or the `mix compile.protocols` task.
4399
+ called protocol consolidation. Consolidation directly links protocols
4400
+ to their implementations in a way that invoking a function from a
4401
+ consolidated protocol is equivalent to invoking two remote functions.
4402
+
4403
+ Protocol consolidation is applied by default to all Mix projects during
4404
+ compilation. This may be an issue during test. For instance, if you want
4405
+ to implement a protocol during test, the implementation will have no
4406
+ effect, as the protocol has already been consolidated. One possible
4407
+ solution is to include compilation directories that are specific to your
4408
+ test environment in your mix.exs:
4409
+
4410
+ def project do
4411
+ ...
4412
+ elixirc_paths: elixirc_paths(Mix.env)
4413
+ ...
4414
+ end
4415
+
4416
+ defp elixirc_paths(:test), do: ["lib", "test/support"]
4417
+ defp elixirc_paths(_), do: ["lib"]
4418
+
4419
+ And then you can define the implementations specific to the test environment
4420
+ inside `test/support/some_file.ex`.
4421
+
4422
+ Another approach is to disable protocol consolidation during tests in your
4423
+ mix.exs:
4424
+
4425
+ def project do
4426
+ ...
4427
+ consolidate_protocols: Mix.env != :test
4428
+ ...
4429
+ end
4430
+
4431
+ Although doing so is not recommended as it may affect your test suite
4432
+ performance.
4433
+
4434
+ Finally note all protocols are compiled with `debug_info` set to `true`,
4435
+ regardless of the option set by `elixirc` compiler. The debug info is
4436
+ used for consolidation and it may be removed after consolidation.
4407
4437
"""
4408
4438
defmacro defprotocol ( name , do_block )
4409
4439
0 commit comments