Index: pjproject/trunk/pjsip/src/test-pjsip/tsx_uas_test.c
===================================================================
--- pjproject/trunk/pjsip/src/test-pjsip/tsx_uas_test.c	(revision 117)
+++ pjproject/trunk/pjsip/src/test-pjsip/tsx_uas_test.c	(revision 119)
@@ -46,22 +46,29 @@
  ** TEST4_BRANCH_ID
  **	Transaction retransmits last response (if any) without notifying 
- **	transaction user upon receiving request  retransmissions on:
- **	    a. TRYING state.
- **	    a. PROCEEDING state.
- **	    b. COMPLETED state.
+ **	transaction user upon receiving request  retransmissions on TRYING
+ **	state
  **
  ** TEST5_BRANCH_ID
- **	INVITE transaction MUST retransmit final response. (Note: PJSIP also
- **	retransmit 2xx final response until it's terminated by user).
+ **	As above, in PROCEEDING state.
  **
  ** TEST6_BRANCH_ID
+ **	As above, in COMPLETED state, with first sending provisional response.
+ **
+ ** TEST7_BRANCH_ID
+ **	INVITE transaction MUST retransmit non-2xx final response.
+ **
+ ** TEST8_BRANCH_ID
+ **	As above, for INVITE's 2xx final response (this is PJSIP specific).
+ **
+ ** TEST9_BRANCH_ID
  **	INVITE transaction MUST cease retransmission of final response when
- *	ACK is received. (Note: PJSIP also retransmit 2xx final response 
- *	until it's terminated by user).
- **
- ** TEST7_BRANCH_ID
+ **	ACK is received. (Note: PJSIP also retransmit 2xx final response 
+ **	until it's terminated by user).
+ **     Transaction also MUST terminate in T4 seconds.
+ **
+ ** TEST10_BRANCH_ID
  **	Test where INVITE UAS transaction never receives ACK
  **
- ** TEST8_BRANCH_ID
+ ** TEST11_BRANCH_ID
  **	When UAS failed to deliver the response with the selected transport,
  **	it should try contacting the client with other transport or begin
@@ -73,5 +80,5 @@
  **	    c. COMPLETED state.
  **
- ** TEST9_BRANCH_ID
+ ** TEST12_BRANCH_ID
  **	Variant of previous test, where transaction fails to deliver the 
  **	response using any kind of transports. Transaction should report
@@ -87,4 +94,9 @@
 static char *TEST6_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAS-Test6";
 static char *TEST7_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAS-Test7";
+static char *TEST8_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAS-Test8";
+static char *TEST9_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAS-Test9";
+static char *TEST10_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAS-Test10";
+static char *TEST11_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAS-Test11";
+static char *TEST12_BRANCH_ID = PJSIP_RFC3261_BRANCH_ID "-UAS-Test12";
 
 #define TEST1_STATUS_CODE	200
@@ -92,5 +104,25 @@
 #define TEST3_PROVISIONAL_CODE	PJSIP_SC_QUEUED
 #define TEST3_STATUS_CODE	202
-
+#define TEST4_STATUS_CODE	200
+#define TEST4_REQUEST_COUNT	2
+#define TEST5_PROVISIONAL_CODE	100
+#define TEST5_STATUS_CODE	200	
+#define TEST5_REQUEST_COUNT	2
+#define TEST5_RESPONSE_COUNT	2
+#define TEST6_PROVISIONAL_CODE	100
+#define TEST6_STATUS_CODE	200	/* Must be final */
+#define TEST6_REQUEST_COUNT	2
+#define TEST6_RESPONSE_COUNT	3
+#define TEST7_STATUS_CODE	301
+#define TEST8_STATUS_CODE	302
+
+
+#define TEST4_TITLE "test4: absorbing request retransmission"
+#define TEST5_TITLE "test5: retransmit last response in PROCEEDING state"
+#define TEST6_TITLE "test6: retransmit last response in COMPLETED state"
+
+
+#define TEST_TIMEOUT_ERROR	-30
+#define MAX_ALLOWED_DIFF	150
 
 static void tsx_user_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e);
@@ -162,4 +194,5 @@
 };
 
+/* Timer callback to send response. */
 static void send_response_timer( pj_timer_heap_t *timer_heap,
 				 struct pj_timer_entry *entry)
@@ -180,4 +213,29 @@
 	PJ_LOG(3,(THIS_FILE,"    error: timer unable to send response"));
 	pjsip_tx_data_dec_ref(r->tdata);
+	return;
+    }
+}
+
+/* Utility to send response. */
+static void send_response( pjsip_rx_data *rdata,
+			   pjsip_transaction *tsx,
+			   int status_code )
+{
+    pj_status_t status;
+    pjsip_tx_data *tdata;
+
+    status = pjsip_endpt_create_response( endpt, rdata, status_code, NULL, 
+					  &tdata);
+    if (status != PJ_SUCCESS) {
+	app_perror("    error: unable to create response", status);
+	test_complete = -196;
+	return;
+    }
+
+    status = pjsip_tsx_send_msg(tsx, tdata);
+    if (status != PJ_SUCCESS) {
+	app_perror("    error: unable to send response", status);
+	pjsip_tx_data_dec_ref(tdata);
+	test_complete = -197;
 	return;
     }
@@ -222,4 +280,47 @@
     }
 }
+
+
+/* Find and terminate tsx with the specified key. */
+static void terminate_our_tsx(int status_code)
+{
+    pjsip_transaction *tsx;
+
+    tsx = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_TRUE);
+    if (!tsx) {
+	PJ_LOG(3,(THIS_FILE,"    error: timer unable to find transaction"));
+	return;
+    }
+
+    pjsip_tsx_terminate(tsx, status_code);
+    pj_mutex_unlock(tsx->mutex);
+}
+
+/* Timer callback to terminate transaction. */
+static void terminate_tsx_timer( pj_timer_heap_t *timer_heap,
+				 struct pj_timer_entry *entry)
+{
+    terminate_our_tsx(entry->id);
+}
+
+
+/* Schedule timer to terminate transaction. */
+static void schedule_terminate_tsx( pjsip_transaction *tsx,
+				    int status_code,
+				    int msec_delay )
+{
+    pj_time_val delay;
+
+    delay.sec = 0;
+    delay.msec = msec_delay;
+    pj_time_val_normalize(&delay);
+
+    pj_assert(pj_strcmp(&tsx->transaction_key, &tsx_key)==0);
+    timer.user_data = NULL;
+    timer.id = status_code;
+    timer.cb = &terminate_tsx_timer;
+    pjsip_endpt_schedule_timer(endpt, &timer, &delay);
+}
+
 
 /*
@@ -332,4 +433,146 @@
 	}
 
+    } else
+    if (pj_strcmp2(&tsx->branch, TEST4_BRANCH_ID)==0) {
+	/*
+	 * TEST4_BRANCH_ID tests receiving retransmissions in TRYING state.
+	 */
+	if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
+
+	    /* Check that status code is status_code. */
+	    if (tsx->status_code != TEST4_STATUS_CODE) {
+		PJ_LOG(3,(THIS_FILE, "    error: incorrect status code"));
+		test_complete = -120;
+	    }
+	    
+	    /* Previous state. */
+	    if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_TRYING) {
+		PJ_LOG(3,(THIS_FILE, "    error: incorrect prev_state"));
+		test_complete = -121;
+	    }
+
+	} else if (tsx->state != PJSIP_TSX_STATE_DESTROYED) 
+	{
+	    PJ_LOG(3,(THIS_FILE, "    error: unexpected state"));
+	    test_complete = -122;
+
+	}
+
+
+    } else
+    if (pj_strcmp2(&tsx->branch, TEST5_BRANCH_ID)==0) {
+	/*
+	 * TEST5_BRANCH_ID tests receiving retransmissions in PROCEEDING state
+	 */
+	if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
+
+	    /* Check that status code is status_code. */
+	    if (tsx->status_code != TEST5_STATUS_CODE) {
+		PJ_LOG(3,(THIS_FILE, "    error: incorrect status code"));
+		test_complete = -130;
+	    }
+	    
+	    /* Previous state. */
+	    if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_PROCEEDING) {
+		PJ_LOG(3,(THIS_FILE, "    error: incorrect prev_state"));
+		test_complete = -131;
+	    }
+
+	} else if (tsx->state == PJSIP_TSX_STATE_PROCEEDING) {
+
+	    /* Check status code. */
+	    if (tsx->status_code != TEST5_PROVISIONAL_CODE) {
+		PJ_LOG(3,(THIS_FILE, "    error: incorrect status code"));
+		test_complete = -132;
+	    }
+
+	} else if (tsx->state != PJSIP_TSX_STATE_DESTROYED) {
+	    PJ_LOG(3,(THIS_FILE, "    error: unexpected state"));
+	    test_complete = -133;
+
+	}
+
+    } else
+    if (pj_strcmp2(&tsx->branch, TEST6_BRANCH_ID)==0) {
+	/*
+	 * TEST6_BRANCH_ID tests receiving retransmissions in COMPLETED state
+	 */
+	if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
+
+	    /* Check that status code is status_code. */
+	    if (tsx->status_code != TEST6_STATUS_CODE) {
+		PJ_LOG(3,(THIS_FILE, "    error: incorrect status code"));
+		test_complete = -140;
+	    }
+	    
+	    /* Previous state. */
+	    if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED) {
+		PJ_LOG(3,(THIS_FILE, "    error: incorrect prev_state"));
+		test_complete = -141;
+	    }
+
+	} else if (tsx->state != PJSIP_TSX_STATE_PROCEEDING &&
+		   tsx->state != PJSIP_TSX_STATE_COMPLETED &&
+		   tsx->state != PJSIP_TSX_STATE_DESTROYED) 
+	{
+	    PJ_LOG(3,(THIS_FILE, "    error: unexpected state"));
+	    test_complete = -142;
+
+	}
+
+
+    } else
+    if (pj_strcmp2(&tsx->branch, TEST7_BRANCH_ID)==0 ||
+	pj_strcmp2(&tsx->branch, TEST8_BRANCH_ID)==0) 
+    {
+	/*
+	 * TEST7_BRANCH_ID and TEST8_BRANCH_ID test retransmission of
+	 * INVITE final response
+	 */
+	int code;
+
+	if (pj_strcmp2(&tsx->branch, TEST7_BRANCH_ID) == 0)
+	    code = TEST7_STATUS_CODE;
+	else
+	    code = TEST8_STATUS_CODE;
+
+	if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
+
+	    if (test_complete == 0)
+		test_complete = 1;
+
+	    /* Check status code. */
+	    if (tsx->status_code != PJSIP_SC_TSX_TIMEOUT) {
+		PJ_LOG(3,(THIS_FILE, "    error: incorrect status code"));
+		test_complete = -150;
+	    }
+	    
+	    /* Previous state. */
+	    if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED) {
+		PJ_LOG(3,(THIS_FILE, "    error: incorrect prev_state"));
+		test_complete = -151;
+	    }
+
+	} else if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
+
+	    /* Check that status code is status_code. */
+	    if (tsx->status_code != code) {
+		PJ_LOG(3,(THIS_FILE, "    error: incorrect status code"));
+		test_complete = -152;
+	    }
+	    
+	    /* Previous state. */
+	    if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_TRYING) {
+		PJ_LOG(3,(THIS_FILE, "    error: incorrect prev_state"));
+		test_complete = -153;
+	    }
+
+	} else if (tsx->state != PJSIP_TSX_STATE_DESTROYED)  {
+
+	    PJ_LOG(3,(THIS_FILE, "    error: unexpected state"));
+	    test_complete = -154;
+
+	}
+
     }
 
@@ -344,4 +587,6 @@
     pj_strcpy(&tsx_key, &key);
 }
+
+#define DIFF(a,b)   ((a<b) ? (b-a) : (a-b))
 
 /*
@@ -372,5 +617,4 @@
 	     */
 	    pjsip_transaction *tsx;
-	    pjsip_tx_data *tdata;
 
 	    status = pjsip_tsx_create_uas(&tsx_user, rdata, &tsx);
@@ -382,20 +626,5 @@
 
 	    save_key(tsx);
-
-	    status = pjsip_endpt_create_response(endpt, rdata, 
-						 status_code, NULL,
-						 &tdata);
-	    if (status != PJ_SUCCESS) {
-		app_perror("    error: unable to create response", status);
-		test_complete = -111;
-		return PJ_TRUE;
-	    }
-
-	    status = pjsip_tsx_send_msg(tsx, tdata);
-	    if (status != PJ_SUCCESS) {
-		app_perror("    error: unable to send response", status);
-		test_complete = -112;
-		return PJ_TRUE;
-	    }
+	    send_response(rdata, tsx, status_code);
 
 	} else {
@@ -428,5 +657,4 @@
 	     */
 	    pjsip_transaction *tsx;
-	    pjsip_tx_data *tdata;
 
 	    status = pjsip_tsx_create_uas(&tsx_user, rdata, &tsx);
@@ -439,20 +667,5 @@
 	    save_key(tsx);
 
-	    status = pjsip_endpt_create_response(endpt, rdata, 
-						 TEST3_PROVISIONAL_CODE, NULL,
-						 &tdata);
-	    if (status != PJ_SUCCESS) {
-		app_perror("    error: unable to create response", status);
-		test_complete = -121;
-		return PJ_TRUE;
-	    }
-
-	    status = pjsip_tsx_send_msg(tsx, tdata);
-	    if (status != PJ_SUCCESS) {
-		app_perror("    error: unable to send response", status);
-		test_complete = -122;
-		return PJ_TRUE;
-	    }
-
+	    send_response(rdata, tsx, TEST3_PROVISIONAL_CODE);
 	    schedule_send_response(rdata, &tsx->transaction_key, 
 				   TEST3_STATUS_CODE, 2000);
@@ -483,4 +696,253 @@
 	return PJ_TRUE;
 
+    } else if (pj_strcmp2(&branch_param, TEST4_BRANCH_ID) == 0 ||
+	       pj_strcmp2(&branch_param, TEST5_BRANCH_ID) == 0 ||
+	       pj_strcmp2(&branch_param, TEST6_BRANCH_ID) == 0) 
+    {
+
+	/* TEST4_BRANCH_ID: absorbs retransmissions in TRYING state. */
+	/* TEST5_BRANCH_ID: retransmit last response in PROCEEDING state. */
+	/* TEST6_BRANCH_ID: retransmit last response in COMPLETED state. */
+
+	if (msg->type == PJSIP_REQUEST_MSG) {
+	    /* On received response, create UAS. */
+	    pjsip_transaction *tsx;
+
+	    status = pjsip_tsx_create_uas(&tsx_user, rdata, &tsx);
+	    if (status != PJ_SUCCESS) {
+		app_perror("    error: unable to create transaction", status);
+		test_complete = -130;
+		return PJ_TRUE;
+	    }
+
+	    save_key(tsx);
+
+	    if (pj_strcmp2(&branch_param, TEST4_BRANCH_ID) == 0) {
+
+	    } else if (pj_strcmp2(&branch_param, TEST5_BRANCH_ID) == 0) {
+		send_response(rdata, tsx, TEST5_PROVISIONAL_CODE);
+
+	    } else if (pj_strcmp2(&branch_param, TEST6_BRANCH_ID) == 0) {
+		send_response(rdata, tsx, TEST6_PROVISIONAL_CODE);
+		send_response(rdata, tsx, TEST6_STATUS_CODE);
+	    }
+
+	} else {
+	    /* Verify the response received. */
+	    
+	    ++recv_count;
+
+	    if (pj_strcmp2(&branch_param, TEST4_BRANCH_ID) == 0) {
+		PJ_LOG(3,(THIS_FILE, "    error: not expecting response!"));
+		test_complete = -132;
+
+	    } else if (pj_strcmp2(&branch_param, TEST5_BRANCH_ID) == 0) {
+
+		if (rdata->msg_info.msg->line.status.code!=TEST5_PROVISIONAL_CODE) {
+		    PJ_LOG(3,(THIS_FILE, "    error: incorrect status code!"));
+		    test_complete = -133;
+
+		} 
+		if (recv_count > TEST5_RESPONSE_COUNT) {
+		    PJ_LOG(3,(THIS_FILE, "    error: not expecting response!"));
+		    test_complete = -134;
+		}
+
+	    } else if (pj_strcmp2(&branch_param, TEST6_BRANCH_ID) == 0) {
+
+		int code = rdata->msg_info.msg->line.status.code;
+
+		switch (recv_count) {
+		case 1:
+		    if (code != TEST6_PROVISIONAL_CODE) {
+			PJ_LOG(3,(THIS_FILE, "    error: invalid code!"));
+			test_complete = -135;
+		    }
+		    break;
+		case 2:
+		case 3:
+		    if (code != TEST6_STATUS_CODE) {
+			PJ_LOG(3,(THIS_FILE, "    error: invalid code!"));
+			test_complete = -136;
+		    }
+		    break;
+		default:
+		    PJ_LOG(3,(THIS_FILE, "    error: not expecting response"));
+		    test_complete = -137;
+		    break;
+		}
+	    }
+	}
+	return PJ_TRUE;
+
+
+    } else if (pj_strcmp2(&branch_param, TEST7_BRANCH_ID) == 0 ||
+	       pj_strcmp2(&branch_param, TEST8_BRANCH_ID) == 0) 
+    {
+
+	/*
+	 * TEST7_BRANCH_ID and TEST8_BRANCH_ID test the retransmission
+	 * of INVITE final response
+	 */
+	if (msg->type == PJSIP_REQUEST_MSG) {
+
+	    /* On received response, create UAS. */
+	    pjsip_transaction *tsx;
+
+	    status = pjsip_tsx_create_uas(&tsx_user, rdata, &tsx);
+	    if (status != PJ_SUCCESS) {
+		app_perror("    error: unable to create transaction", status);
+		test_complete = -140;
+		return PJ_TRUE;
+	    }
+
+	    save_key(tsx);
+
+	    if (pj_strcmp2(&branch_param, TEST7_BRANCH_ID) == 0) {
+
+		send_response(rdata, tsx, TEST7_STATUS_CODE);
+
+	    } else {
+
+		send_response(rdata, tsx, TEST8_STATUS_CODE);
+
+	    }
+
+	} else {
+	    int code;
+
+	    ++recv_count;
+
+	    if (pj_strcmp2(&branch_param, TEST7_BRANCH_ID) == 0)
+		code = TEST7_STATUS_CODE;
+	    else
+		code = TEST8_STATUS_CODE;
+
+	    if (recv_count==1) {
+		
+		if (rdata->msg_info.msg->line.status.code != code) {
+		    PJ_LOG(3,(THIS_FILE,"    error: invalid status code"));
+		    test_complete = -141;
+		}
+
+		recv_last = rdata->pkt_info.timestamp;
+
+	    } else {
+
+		pj_time_val now;
+		unsigned msec, msec_expected;
+
+		now = rdata->pkt_info.timestamp;
+
+		PJ_TIME_VAL_SUB(now, recv_last);
+	    
+		msec = now.sec*1000 + now.msec;
+		msec_expected = (1 << (recv_count-2)) * PJSIP_T1_TIMEOUT;
+		if (msec_expected > PJSIP_T2_TIMEOUT)
+		    msec_expected = PJSIP_T2_TIMEOUT;
+
+		if (DIFF(msec, msec_expected) > MAX_ALLOWED_DIFF) {
+		    PJ_LOG(3,(THIS_FILE,
+			      "    error: incorrect retransmission "
+			      "time (%d ms expected, %d ms received",
+			      msec_expected, msec));
+		    test_complete = -142;
+		}
+
+		if (recv_count > 11) {
+		    PJ_LOG(3,(THIS_FILE,"    error: too many responses (%d)",
+					recv_count));
+		    test_complete = -143;
+		}
+
+		recv_last = rdata->pkt_info.timestamp;
+	    }
+
+	}
+	return PJ_TRUE;
+
+    } else if (pj_strcmp2(&branch_param, TEST9_BRANCH_ID)) {
+
+	/*
+	 * TEST9_BRANCH_ID tests that the retransmission of INVITE final 
+	 * response should cease when ACK is received. Transaction also MUST
+	 * terminate in T4 seconds.
+	 */
+	if (msg->type == PJSIP_REQUEST_MSG) {
+
+	    /* On received response, create UAS. */
+	    pjsip_transaction *tsx;
+
+	    status = pjsip_tsx_create_uas(&tsx_user, rdata, &tsx);
+	    if (status != PJ_SUCCESS) {
+		app_perror("    error: unable to create transaction", status);
+		test_complete = -140;
+		return PJ_TRUE;
+	    }
+
+	    save_key(tsx);
+
+	    if (pj_strcmp2(&branch_param, TEST7_BRANCH_ID) == 0) {
+
+		send_response(rdata, tsx, TEST7_STATUS_CODE);
+
+	    } else {
+
+		send_response(rdata, tsx, TEST8_STATUS_CODE);
+
+	    }
+
+	} else {
+	    int code;
+
+	    ++recv_count;
+
+	    if (pj_strcmp2(&branch_param, TEST7_BRANCH_ID) == 0)
+		code = TEST7_STATUS_CODE;
+	    else
+		code = TEST8_STATUS_CODE;
+
+	    if (recv_count==1) {
+		
+		if (rdata->msg_info.msg->line.status.code != code) {
+		    PJ_LOG(3,(THIS_FILE,"    error: invalid status code"));
+		    test_complete = -141;
+		}
+
+		recv_last = rdata->pkt_info.timestamp;
+
+	    } else {
+
+		pj_time_val now;
+		unsigned msec, msec_expected;
+
+		now = rdata->pkt_info.timestamp;
+
+		PJ_TIME_VAL_SUB(now, recv_last);
+	    
+		msec = now.sec*1000 + now.msec;
+		msec_expected = (1 << (recv_count-2)) * PJSIP_T1_TIMEOUT;
+		if (msec_expected > PJSIP_T2_TIMEOUT)
+		    msec_expected = PJSIP_T2_TIMEOUT;
+
+		if (DIFF(msec, msec_expected) > MAX_ALLOWED_DIFF) {
+		    PJ_LOG(3,(THIS_FILE,
+			      "    error: incorrect retransmission "
+			      "time (%d ms expected, %d ms received",
+			      msec_expected, msec));
+		    test_complete = -142;
+		}
+
+		if (recv_count > 11) {
+		    PJ_LOG(3,(THIS_FILE,"    error: too many responses (%d)",
+					recv_count));
+		    test_complete = -143;
+		}
+
+		recv_last = rdata->pkt_info.timestamp;
+	    }
+
+	}
+	return PJ_TRUE;
     }
 
@@ -493,10 +955,13 @@
 static int perform_test( char *target_uri, char *from_uri, 
 			 char *branch_param, int test_time, 
-			 const pjsip_method *method )
+			 const pjsip_method *method,
+			 int request_cnt, int request_interval_msec,
+			 int expecting_timeout)
 {
     pjsip_tx_data *tdata;
     pj_str_t target, from;
     pjsip_via_hdr *via;
-    pj_time_val timeout;
+    pj_time_val timeout, next_send;
+    int sent_cnt;
     pj_status_t status;
 
@@ -527,15 +992,8 @@
     via->branch_param = pj_str(branch_param);
 
-    /* Add additional reference to tdata to prevent transaction from
-     * deleting it.
-     */
-    pjsip_tx_data_add_ref(tdata);
-
-    /* Send the first message. */
-    status = pjsip_endpt_send_request_stateless(endpt, tdata, NULL, NULL);
-    if (status != PJ_SUCCESS) {
-	app_perror("   Error: unable to send request", status);
-	return -20;
-    }
+    /* Schedule first send. */
+    sent_cnt = 0;
+    pj_gettimeofday(&next_send);
+    pj_time_val_normalize(&next_send);
 
     /* Set test completion time. */
@@ -550,8 +1008,33 @@
 
 	pj_gettimeofday(&now);
+
+	if (sent_cnt < request_cnt && PJ_TIME_VAL_GTE(now, next_send)) {
+	    /* Add additional reference to tdata to prevent transaction from
+	     * deleting it.
+	     */
+	    pjsip_tx_data_add_ref(tdata);
+
+	    /* (Re)Send the request. */
+	    status = pjsip_endpt_send_request_stateless(endpt, tdata, 0, 0);
+	    if (status != PJ_SUCCESS) {
+		app_perror("   Error: unable to send request", status);
+		pjsip_tx_data_dec_ref(tdata);
+		return -20;
+	    }
+
+	    /* Schedule next send, if any. */
+	    sent_cnt++;
+	    if (sent_cnt < request_cnt) {
+		pj_gettimeofday(&next_send);
+		next_send.msec += request_interval_msec;
+		pj_time_val_normalize(&next_send);
+	    }
+	}
+
 	if (now.sec > timeout.sec) {
-	    PJ_LOG(3,(THIS_FILE, "   Error: test has timed out"));
+	    if (!expecting_timeout)
+		PJ_LOG(3,(THIS_FILE, "   Error: test has timed out"));
 	    pjsip_tx_data_dec_ref(tdata);
-	    return -30;
+	    return TEST_TIMEOUT_ERROR;
 	}
     }
@@ -613,5 +1096,5 @@
 			  TEST1_BRANCH_ID,
 			  33, /* Test duration must be greater than 32 secs */
-			  &pjsip_options_method);
+			  &pjsip_options_method, 1, 0, 0);
     if (status != 0)
 	return status;
@@ -623,5 +1106,5 @@
 			  TEST2_BRANCH_ID,
 			  33, /* Test duration must be greater than 32 secs */
-			  &pjsip_options_method);
+			  &pjsip_options_method, 1, 0, 0);
     if (status != 0)
 	return status;
@@ -647,7 +1130,86 @@
 			  TEST3_BRANCH_ID,
 			  35,
-			  &pjsip_options_method);
+			  &pjsip_options_method, 1, 0, 0);
+
     return status;
 }
+
+
+/*****************************************************************************
+ **
+ ** TEST4_BRANCH_ID: Absorbs retransmissions in TRYING state
+ ** TEST5_BRANCH_ID: Absorbs retransmissions in PROCEEDING state
+ ** TEST6_BRANCH_ID: Absorbs retransmissions in COMPLETED state
+ **
+ *****************************************************************************
+ */
+static int tsx_retransmit_last_response_test(const char *title,
+					     char *branch_id,
+					     int request_cnt,
+					     int status_code)
+{
+    int status;
+
+    PJ_LOG(3,(THIS_FILE,"  %s", title));
+
+    status = perform_test("sip:129.0.0.1;transport=loop-dgram",
+		          "sip:129.0.0.1;transport=loop-dgram",
+			  branch_id,
+			  5,
+			  &pjsip_options_method, 
+			  request_cnt, 1000, 1);
+    if (status && status != TEST_TIMEOUT_ERROR)
+	return status;
+    if (!status) {
+	PJ_LOG(3,(THIS_FILE, "   error: expecting timeout"));
+	return -31;
+    }
+
+    terminate_our_tsx(status_code);
+    flush_events(100);
+
+    if (test_complete != 1)
+	return test_complete;
+
+    flush_events(100);
+    return 0;
+}
+
+/*****************************************************************************
+ **
+ ** TEST7_BRANCH_ID: INVITE non-2xx final response retransmission test
+ ** TEST8_BRANCH_ID: INVITE 2xx final response retransmission test
+ **
+ *****************************************************************************
+ */
+static int tsx_final_response_retransmission_test(void)
+{
+    int status;
+
+    PJ_LOG(3,(THIS_FILE,
+	      "  test7: INVITE non-2xx final response retransmission"));
+
+    status = perform_test("sip:129.0.0.1;transport=loop-dgram",
+		          "sip:129.0.0.1;transport=loop-dgram",
+			  TEST7_BRANCH_ID,
+			  33, /* Test duration must be greater than 32 secs */
+			  &pjsip_invite_method, 1, 0, 0);
+    if (status != 0)
+	return status;
+
+    PJ_LOG(3,(THIS_FILE,
+	      "  test8: INVITE 2xx final response retransmission"));
+
+    status = perform_test("sip:129.0.0.1;transport=loop-dgram",
+		          "sip:129.0.0.1;transport=loop-dgram",
+			  TEST8_BRANCH_ID,
+			  33, /* Test duration must be greater than 32 secs */
+			  &pjsip_invite_method, 1, 0, 0);
+    if (status != 0)
+	return status;
+
+    return 0;
+}
+
 
 
@@ -690,5 +1252,4 @@
     if (status != 0)
 	return status;
-#endif
 
     /* TEST3_BRANCH_ID: with provisional response
@@ -698,4 +1259,40 @@
 	return status;
 
+    /* TEST4_BRANCH_ID: absorbs retransmissions in TRYING state
+     */
+    status = tsx_retransmit_last_response_test(TEST4_TITLE,
+					       TEST4_BRANCH_ID, 
+					       TEST4_REQUEST_COUNT,
+					       TEST4_STATUS_CODE);
+    if (status != 0)
+	return status;
+
+    /* TEST5_BRANCH_ID: retransmit last response in PROCEEDING state
+     */
+    status = tsx_retransmit_last_response_test(TEST5_TITLE,
+					       TEST5_BRANCH_ID, 
+					       TEST5_REQUEST_COUNT,
+					       TEST5_STATUS_CODE);
+    if (status != 0)
+	return status;
+
+    /* TEST6_BRANCH_ID: retransmit last response in PROCEEDING state
+     */
+    status = tsx_retransmit_last_response_test(TEST6_TITLE,
+					       TEST6_BRANCH_ID, 
+					       TEST6_REQUEST_COUNT,
+					       TEST6_STATUS_CODE);
+    if (status != 0)
+	return status;
+
+    /* TEST7_BRANCH_ID: INVITE non-2xx final response retransmission test
+     * TEST8_BRANCH_ID: INVITE 2xx final response retransmission test
+     */
+
+    status = tsx_final_response_retransmission_test();
+    if (status != 0)
+	return status;
+
+#endif
 
     pjsip_transport_dec_ref(loop);
