Erlang のアプリケーション構築開始

やったこと

  • ドキュメントを読んでディレクトリを作った。ebin/src/log/test 。
  • supervisor, gen_server, application それぞれの behaviour のひなたがを作った
  • Common Test を導入して簡単なテストを書いた
  • Makefile を整備
    • make
    • make check
  • 未解決
    • supervisor が child process を自動起動してくれない
    • 解決:simple_one_for_one => one_for_one

supervisor

Supervisor Behaviourを読む。
ほぼサンプルの通り。restart strategies を指定する。

-module(mio_sup).
-behaviour(supervisor).

-export([start_link/0]).
-export([init/1]).

-define(SERVER, ?MODULE).

start_link() ->
    %% register local as ?SERVER.
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

init(_Args) ->
    {ok, {{simple_one_for_one, 0, 1},
          [{mio, {mio, start_link, []},
            temporary, brutal_kill, worker, [mio]}]}}.

gen_server

Emacs erl-mode の skelton 機能でコード生成。

起動フロー

  1. application:start(mio)
  2. mio_sup:start_link()
  3. mio_node:start_link

3番目の start_link が自動起動しない。Child Specification が間違っているかな。
うーん。ドキュメントを読んでもよく分からないな。
supervisor は child を自動起動するように読めるんだけど。
しょうがないので
supervisor:start_child(mio_sup, []). と明示的に start_child した。

gen_server:call してみる

gen_server:call(mio_node, hige).
ok

うん。期待通り。

Common Test

テストフレームワークが欲しいので Common Test を使ってみる。
インストール。環境は OSX 10.5.7 。参考 id:cooldaemon:20080118:1200635774 氏のよいまとめ。

% sudo mkdir -p /usr/local/lib/erlang/lib/common_test-1.4.1/priv/bin
% cd /usr/local/lib/erlang/lib/common_test-1.4.1/
% sudo ./install.sh local
install successful, start script created in  local/common_test-1.4.1/priv/bin	


Emacs の skelton で生成し、要らないコードを削った。

-module(mio_node_SUITE).

-compile(export_all).

init_per_suite(Config) ->
    mio_sup:start_link(),
    supervisor:start_child(mio_sup, []),
    Config.

end_per_suite(Config) ->
    ok.

all() -> 
    [my_test_case].

my_test_case() ->
    [].

my_test_case(_Config) -> 
    ok = gen_server:call(mio_node, hige),
    ok.


実行。

% /usr/local/lib/erlang/lib/common_test-1.4.1/priv/bin/run_test -dir . -logdir ./log -pa ~/higepon/mio/ebin

pa オプションはフルパスでなければならない。これにハマった。


ちなみに mio_node_SUITE が mio_sup を link しているので EXIT メッセージがきて死んでしまう。
どうしよう。

=ERROR REPORT==== 30-Jun-2009::17:40:59 ===
** Generic server mio_sup terminating 
** Last message in was {'EXIT',<0.68.0>,
                               {#Ref<0.0.0.693>,1122,ok,
                                {mio_node_SUITE,my_test_case},
                                []}}

直後に unlink することにした。

    {ok, Pid} = mio_sup:start_link(),
    unlink(Pid),