// Tests simple send and recv (1 packet each direction), // two threads waiting on same port, sequence of messages sent across #include "minithread.h" #include "synch.h" #include "minimsg.h" #include #include #include #include network_address_t my_address; int bool2, bool3, error; int testthread(int* arg); int thread1(int *arg); int thread2(int *arg); int thread3(int *arg); long mytime(); void wait(long timeout); void main (void){ minithread_system_initialize(testthread, NULL); } int testthread(int* arg){ miniport_t port1; long timeout; printf("\n\nTesting the minimsg interface\n\n"); network_get_my_address(my_address); // miniport_local_create port1 = miniport_local_create(); // Two threads printf("\n\n1. Send-recv between two threads...\n"); timeout = 100000; //100 sec if(minithread_fork(thread1, NULL) == NULL) printf("\nError - Unable to fork thread1"); else if(minithread_fork(thread2, (int *)port1) == NULL) printf("\nError - Unable to fork thread2"); wait(timeout); // Several threads // other tests - two people waiting on same port //end return 0; } int thread1(int *arg){ miniport_t myport, remport, remport2, port1; char buff[20]; int len=20; char str[10][10]; int i; // Normal send-recv routine myport = miniport_local_create(); strcpy(buff, "testing"); remport = miniport_remote_create(my_address, 0); printf("\nthread1: 1st send"); if(minimsg_send(myport, remport, buff, 8) != 8) printf("\n minimsg_send return value error"); //len=20; if(minimsg_receive(myport, &remport2, buff, &len) != 10) printf("\n minimsg_recv return value error"); if(strcmp(buff, "testagain") == 0 && len == 10) printf("..thread1: 2nd send recv'd ok\n"); else printf("..thread1: 2nd send not recv'd ok\n"); // minimsg_receive() creating new miniport ? if(remport2 != remport) printf("\n..error: trying to create a new struct miniport remote on minimsg_receive\n"); // using a destroyed port printf("\n2. Destroying a local port.."); miniport_destroy(myport); /* if(minimsg_send(myport, myport, buff, 2) >= 0) printf("Error: minimsg_send able to send on destroyed local port"); if (minimsg_receive(myport, &remport, buff, &len) >= 0) printf("Error: minimsg_receive able to receive on destroyed local port"); */ // Third thread - multiple threads doing minimsg_receive() on same port printf("\n\n3. Testing with two threads waiting on one port "); myport = miniport_local_create(); //Three threads strcpy(buff, "thread2"); minimsg_send(myport, myport, buff, 8); strcpy(buff, "thread3"); minimsg_send(myport, myport, buff, 8); if(minithread_fork(thread3, (int *)myport) == NULL) printf("\nError - unable to fork thread3"); bool2 = bool3 = error = 0; len=20; minimsg_receive(myport, &remport, buff, &len); if(strcmp(buff, "thread2") == 0) bool2++; else if (strcmp(buff, "thread3") == 0) bool3++; else error++; wait(5000); if(error) printf("..error: bogus message received"); else if (bool2 == 1 && bool3 == 1) printf("..success: each message received at exactly one waiting minithread"); else if(bool2 >= 2 || bool3 >= 2) printf("..error: message copied to multiple minithreads on same port"); else printf("..error: message sent and lost"); //Stream of essages port1 = miniport_local_create(); error = 0; printf("\n\n4. Testing with a stream of messages..."); for(i = 0; i <= 9; i++){ strcpy(str[i], "test"); str[i][4] = (char )i; str[i][5] = '\0'; minimsg_send(port1, port1, str[i], strlen(str[i])+1); } for(i = 0; i <= 9; i++){ len = 20; minimsg_receive(port1, &remport, buff, &len); if(strcmp (buff, str[i]) != 0) error = 1; } if(error == 0) printf("...success: all messages received"); else printf("...failed: bogus or lost messages"); return 0; } int thread2(int *arg){ miniport_t myport, remport; char buff[10]; int len=10; myport = (miniport_t) arg; minimsg_receive(myport, &remport, buff, &len); if(strcmp(buff, "testing") == 0 && len == 8) printf("..thread2: 1st send recv'd ok\n"); else printf("..thread2: 1st send not recv'd ok\n"); strcpy(buff, "testagain"); printf("\nthread2: 2nd send back"); minimsg_send(myport, remport, buff, 10); return 0; } int thread3(int *arg){ miniport_t myport = (miniport_t) arg; miniport_t remport; char buff[100]; int len=100; minimsg_receive(myport, &remport, buff, &len); if(strcmp(buff, "thread2") == 0) bool2++; else if (strcmp(buff, "thread3") == 0) bool3++; else error++; // end return 0; } // return relative time in millisecs long mytime(){ return ((long) clock()); } // wait for timeout millisecs void wait(long timeout){ long now = mytime(); while(now+timeout > mytime()) ; }