anchor.diff:      {error, _} = start_check_actions([CheckAction_Wait1, CheckAction_Wait2]),
   
   
  +    %% test start_actions(BranchBase, Parent, OrigRequest, Actions)
  +    %%--------------------------------------------------------------------
  +    autotest:mark(?LINE, "start_actions/4 - 1.1"),
  +    %% test normal case, but with too low Max-Forwards for a fork to actually happen
  +    StartAcReq1 = #request{method = "OPTIONS", uri = sipurl:parse("sip:ft@testcase.example.org"),
  +			   header = keylist:from_list([{"Max-Forwards", ["1"]}]), body = <<>>},
  +    error = start_actions("testbranchbase", self(), StartAcReq1, [CheckAction_Call1, CheckAction_Wait1]),
  +
  +    autotest:mark(?LINE, "start_actions/4 - 1.2"),
  +    check_we_got_signal({sipproxy_all_terminated, Self, {483, "Too Many Hops"}}),
  +    check_we_got_signal({sipproxy_terminating, Self}),
  +
  +    autotest:mark(?LINE, "start_actions/4 - 2"),
  +    %% test failure case
  +    error = start_actions("foo", self(), #request{}, []),
  +
  +
  +    %% test start(BranchBase, Parent, Request, Actions, Timeout)
  +    %%--------------------------------------------------------------------
  +    autotest:mark(?LINE, "start/5 - 0"),
  +    StartRef = make_ref(),
  +    StartSigCol = spawn_link(?MODULE, signal_collector, [self(), StartRef, passive]),
  +
  +    autotest:mark(?LINE, "start/5 - 1"),
  +    %% Test with Route header
  +    StartReq1 = #request{method = "OPTIONS", uri = sipurl:parse("sip:ft@testcase.example.org"),
  +			 header = keylist:from_list([{"Route", ["<sip:testcase.example.org>"]}]), body = <<>>},
  +    {error, "Request with Route header could not be forked"} =
  +	start("testbranchbase", StartSigCol, StartReq1, [], 0),
  +    StartExpectedSignals1 = [{sipproxy_all_terminated, Self,
  +			      {500, "Server Internal Error"}},
  +			     {sipproxy_terminating, Self}],
  +    poll_signal_collector(StartRef, StartSigCol, StartExpectedSignals1),
  +
  +    autotest:mark(?LINE, "start/5 - 2.0"),
  +    %% Test normal case (but only wait action, so a 500 will be generated)
  +    StartReq2 = #request{method = "OPTIONS", uri = sipurl:parse("sip:ft@testcase.example.org"),
  +			 header = keylist:from_list([]), body = <<>>},
  +
  +    autotest:mark(?LINE, "start/5 - 2.1"),
  +    ok = start("testbranchbase", StartSigCol, StartReq2, [#sipproxy_action{action = wait, timeout = 0}], 0),
  +
  +    autotest:mark(?LINE, "start/5 - 2.2"),
  +    StartExpectedSignals2 = [{sipproxy_all_terminated, Self,
  +			      {500, "Server Internal Error"}},
  +			     {sipproxy_terminating, Self}],
  +    poll_signal_collector(StartRef, StartSigCol, StartExpectedSignals2),
  +
  +    StartSigCol ! {quit, self()},
  +
  +
  +
  +    %% test fork(EndTime, State)
  +    %%--------------------------------------------------------------------
  +    autotest:mark(?LINE, "fork/2 - 0"),
  +    %% test that we send a 408 Request Timeout if we reach our timeout and
  +    %% haven't sent a final response yet. This can happen if one of our
  +    %% branches has been cancelled but have not received a response yet.
  +    ForkList0 = targetlist:empty(),
  +    ForkRequest1 = #request{method = "INVITE", uri = sipurl:parse("sip:ft@example.org"),
  +			    header = keylist:from_list([]), body = <<>>},
  +    %% start a signal collector
  +    ForkRef1 = make_ref(),
  +    ForkSigCollect1 = spawn_link(?MODULE, signal_collector, [self(), ForkRef1, passive]),
  +    ForkList1_1 = targetlist:add("branch1", ForkRequest1, ForkSigCollect1, calling,
  +			       1, [], ForkList0),
  +    ForkList1 = targetlist:add("branch2", ForkRequest1, ForkSigCollect1, terminated,
  +			       1, [], ForkList1_1),
  +
  +    autotest:mark(?LINE, "fork/2 - 1.1"),
  +    ForkState1 = #state{request = ForkRequest1,
  +			timeout = 0,
  +			parent  = ForkSigCollect1,
  +			targets = ForkList1,
  +			actions = []
  +		       },
  +    ok = fork(0, ForkState1),
  +
  +    autotest:mark(?LINE, "fork/2 - 1.2"),
  +    ForkCancelCast = {'$gen_cast', {cancel, "cancel_targets_state", []}},
  +    ForkExpectSignals1 = [ForkCancelCast,
  +			  {sipproxy_no_more_actions, Self},
  +			  {sipproxy_all_terminated, Self, {408, "Request Timeout"}}
  +			 ],
  +    poll_signal_collector(ForkRef1, ForkSigCollect1, ForkExpectSignals1),
  +
  +    autotest:mark(?LINE, "fork/2 - 2.1"),
  +    %% test all terminated
  +    ForkList2 = targetlist:add("terminatedbranch", ForkRequest1, ForkSigCollect1, terminated,
  +			       1, [], ForkList0),
  +    ForkState2 = #state{request = ForkRequest1,
  +			timeout = 0,
  +			parent  = ForkSigCollect1,
  +			targets = ForkList2,
  +			actions = [],
  +			final_response_sent = true
  +		       },
  +    ok = fork(0, ForkState2),
  +
  +    autotest:mark(?LINE, "fork/2 - 2.2"),
  +    %% verify that no signals were sent to the signal collector
  +    poll_signal_collector(ForkRef1, ForkSigCollect1, []),
  +
  +    autotest:mark(?LINE, "fork/2 - 3.1"),
  +    %% test generation of 500 Server Internal Error when no final repsonse has been generated,
  +    %% for whatever reason
  +    ForkState3 = ForkState2#state{final_response_sent = false},
  +    ok = fork(0, ForkState3),
  +
  +    autotest:mark(?LINE, "fork/2 - 3.2"),
  +    ForkExpectedSignals3 = [{sipproxy_all_terminated, Self, {500, "Server Internal Error"}}],
  +    poll_signal_collector(ForkRef1, ForkSigCollect1, ForkExpectedSignals3),
  +
  +
  +    ForkSigCollect1 ! {quit, self()},
  +
  +
       %% test get_next_target_branch(In)
       %%--------------------------------------------------------------------
       autotest:mark(?LINE, "get_next_target_branch - 1"),
  
positive.code:     TargetsCalling = targetlist:get_targets_in_state(calling, TargetList),
      TargetsTrying = targetlist:get_targets_in_state(trying, TargetList),
      TargetsProceeding = targetlist:get_targets_in_state(proceeding, TargetList),
      %%TargetsCompleted = targetlist:get_targets_in_state(completed, TargetList),
      if
  	TargetsCalling /= [] -> false;
  	TargetsTrying /= [] -> false;
  	TargetsProceeding /= [] -> false;
  	%%TargetsCompleted /= [] -> false;
  	true -> true
      end.
  
  
negative.code: 	wait ->
  	    Time = HAction#sipproxy_action.timeout,
  	    logger:log(debug, "sipproxy: waiting ~p seconds", [Time]),
  	    %% XXX we loose our original endtime that is starttime + timeout given to start() here,
  	    %% we probably shouldn't. The meaning of the timeout to start() is not clearly specified.
  	    NewEndTime = util:timestamp() + Time,
  	    case process_wait(NewEndTime, State) of
  		{discontinue, NewState} when is_record(NewState, state) ->
  		    logger:log(debug, "sipproxy: discontinue processing"),
  		    fork(NewEndTime, NewState#state{actions = []});
  		{timeout, NewState} when is_record(NewState, state) ->
  		    fork(NewEndTime, NewState#state{actions = TAction})
  	    end
      end.
  