我目前正在努力处理混合中的配置值(特别是在运行测试时).这是我的情景:
test.exs档案/config我现在正在这样做(这不起作用).正在测试的模块(简化):
defmodule Streamex.Client do
@api_region Application.get_env(:streamex, :region)
@api_key Application.get_env(:streamex, :key)
@api_secret Application.get_env(:streamex, :secret)
@api_version "v1.0"
@api_url "api.getstream.io/api"
def full_url(%Request{} = r) do
url = <<"?api_key=", @api_key :: binary>>
end
end
Run Code Online (Sandbox Code Playgroud)
测试:
setup_all do
Streamex.start
Application.put_env :streamex, :key, "KEY"
Application.put_env :streamex, :secret, "SECRET"
Application.put_env :streamex, :secret, ""
end
Run Code Online (Sandbox Code Playgroud)
运行时会发生的情况mix test是主模块(从这些值设置属性)会抛出以下错误,因为它找不到有效值:
lib/streamex/client.ex:36: invalid literal nil in <<>>
Run Code Online (Sandbox Code Playgroud)
我还在开始,所以这看起来很明显,但在阅读完文档后我找不到解决方案.
我目前正在编写"Programming Elixir"一书,刚刚发现这个以"并行"方式实现的"map"示例:
defmodule Parallel do
def pmap(collection, fun) do
me = self
collection
|> Enum.map(fn (elem) ->
spawn_link fn -> (send me, { self, fun.(elem) }) end end)
|> Enum.map(fn (pid) ->
receive do { ^pid, result } -> result end
end)
end
end
Run Code Online (Sandbox Code Playgroud)
对代码的评论说:"注意它如何^pid在接收块中依次获取每个PID的结果.如果没有这个,我们将以随机顺序返回结果." 有人可以澄清一下吗?