-
Notifications
You must be signed in to change notification settings - Fork 2
/
applicationTest.c
100 lines (79 loc) · 2.36 KB
/
applicationTest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "include/application.h"
void startTest(){
printf("-----------TEST CASES-----------\n");
testBidirectionalComunication();
testRedistributionOfOrders();
}
void testBidirectionalComunication(){
char message[MAX_LENGTH];
char messageReturned[MAX_LENGTH];
int pipeFatherToChild[2];
int pipeChildToFather[2];
givenString(message);
whenSlaveIsExecuted(pipeFatherToChild, pipeChildToFather);
printf("Done execution\n");
whenStringIsSentToSlave(message, pipeFatherToChild);
printf("Message sent\n");
whenStringIsReturned(messageReturned, pipeChildToFather);
thenStringIsReturned(message, messageReturned);
}
void testRedistributionOfOrders(){
queue_o orderQueue;
slaves_o * slaves;
int files = 0;
int i;
int slavesQuantity;
orderQueue = newQueue();
files = loadFiles(DIRNAMETEST, orderQueue, files);
slavesQuantity = files/(2*ORDERS_NUM);
slaves = createSlaves(slavesQuantity);
free(slaves);
for(i = 0; i < files; i++){
node_o * temp = deQueue(orderQueue);
free(temp->order.filename);
free(temp);
}
free(orderQueue);
}
void givenString(char * message){
printf("Write a message to slave: ");
scanf("%s", message);
}
void whenSlaveIsExecuted(int * pipeFatherToChild, int * pipeChildToFather){
int pid;
pipe(pipeFatherToChild);
pipe(pipeChildToFather);
pid = fork();
switch(pid){
case -1: perror("There was an error creating the child");
wait(NULL);
break;
case 0: dup2(pipeFatherToChild[0], STDIN_FILENO);
close(pipeFatherToChild[1]);
dup2(pipeChildToFather[1], STDOUT_FILENO);
close(pipeChildToFather[0]);
execlp(SLAVE_EXEC, SLAVE_EXEC, "arg", NULL);
break;
default: close(pipeFatherToChild[0]);
close(pipeChildToFather[1]);
break;
}
}
void whenStringIsSentToSlave(char * message, int * pipeFatherToChild){
write(pipeFatherToChild[1], message, strlen(message) + 1);
}
void whenStringIsReturned(char * messageReturned, int * pipeChildToFather){
char * current = messageReturned;
while(read(pipeChildToFather[0], current, 1) == 1){
current++;
}
}
void thenStringIsReturned(const char * message, const char * messageReturned){
if(! strcmp(message,messageReturned)){
printf("Success in sending and receiving the message!!!\n");
}else{
printf("Failed to send and receive the message\n");
printf("Original message: %s\n", message);
printf("Message returned: %s\n", messageReturned);
}
}