[erlang-questions] Ensuring a process is crashed with raise/3 in a Common_Test case

Edmond Begumisa ebegumisa@REDACTED
Wed Aug 15 14:40:36 CEST 2018


On Wed, 15 Aug 2018 14:17:40 +1000, Jared Davison  
<jared@REDACTED> wrote:

> Hi,
>
> Could you please assist me in using common test.
>
> I'm trying to ensure that a gen_statem:call/3 raises a {timeout, _} in a  
> common test case.
>
> At first I tried a catch because I thought it would be a throw but it  
> catches nothing.

Sounds like you're catching the wrong exception class. For gen_* timeouts,  
I believe the exception class is 'exit' rather than 'throw'.


> Digging into the gen_statem:call/3 code it appears that it uses  
> erlang:raise/3 which stops the >execution of the process, so that makes  
> sense that catch does nothing.

Catch should do something if you're catching the right exception class  
('exit') or matching any exception class ('_'). It is still possible to  
catch exceptions generated using erlang:raise/3 for any class  
('error'|'throw'|'exit'). E.g from an erlang shell ...

     $ try erlang:raise(exit, foo, []) catch exit:foo -> yep end.
      	
... returns 'yep'. As do...

     $ try erlang:raise(throw, foo, []) catch throw:foo -> yep end.
     $ try erlang:raise(error, foo, []) catch error:foo -> yep end.

... so do...

     $ try exit(foo) catch exit:foo -> yep end.
     $ try throw(foo) catch throw:foo -> yep end.
     $ try error(foo) catch error:foo -> yep end.

... and also ...

     $ try erlang:raise(exit, foo, []) catch _:foo -> yep end.
     $ try erlang:raise(error, foo, []) catch _:foo -> yep end.
     $ try erlang:raise(throw, foo, []) catch _:foo -> yep end.
     $ try exit(foo) catch _:foo -> yep end.
     $ try throw(foo) catch _:foo -> yep end.
     $ try error(foo) catch _:foo -> yep end.

> I want to be sure that the process was stopped with the a particular  
> reason, and pass the test if >it does.
> Is this possible in Common Test?

Should be. My guess is that your test case is catching something like so...

     <snip>

     try gen_statem:call(foo, bar, 5000)
     catch throw:{timeout,_} -> ..
     end.

     </snip>

Try changing that to...

     <snip>

     try gen_statem:call(foo, bar, 5000)
     catch exit:{timeout,_} -> ..
     end.

     </snip>

- Edmond -

>
> Thanks
>
> Jared
>
>
>



-- 
Using Opera's mail client: http://www.opera.com/mail/



More information about the erlang-questions mailing list