sl@0
|
1 |
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// Test code for multi-threaded file descriptors etc.
|
sl@0
|
15 |
//
|
sl@0
|
16 |
//
|
sl@0
|
17 |
|
sl@0
|
18 |
#include <e32std.h>
|
sl@0
|
19 |
#include <e32svr.h> // for RDebug
|
sl@0
|
20 |
#include <stdlib.h>
|
sl@0
|
21 |
#include <unistd.h>
|
sl@0
|
22 |
#include <stdio.h>
|
sl@0
|
23 |
#include <string.h>
|
sl@0
|
24 |
#include <sys/fcntl.h>
|
sl@0
|
25 |
#include <sys/errno.h>
|
sl@0
|
26 |
|
sl@0
|
27 |
extern "C" {
|
sl@0
|
28 |
#include "CTEST.H"
|
sl@0
|
29 |
}
|
sl@0
|
30 |
|
sl@0
|
31 |
#include <estlib.h> // for multi-threading control
|
sl@0
|
32 |
|
sl@0
|
33 |
|
sl@0
|
34 |
#ifdef _DEBUG
|
sl@0
|
35 |
#define ttest_Next(testname) \
|
sl@0
|
36 |
RDebug::Print(_L("t%d: %s"), (TInt)thread, testname); \
|
sl@0
|
37 |
printf("t%d: ", (TInt)thread); \
|
sl@0
|
38 |
test_Next(testname)
|
sl@0
|
39 |
#else
|
sl@0
|
40 |
//don't use RDebug::Print when it's a release build
|
sl@0
|
41 |
#define ttest_Next(testname) test_Next(testname)
|
sl@0
|
42 |
#endif
|
sl@0
|
43 |
|
sl@0
|
44 |
test_Data;
|
sl@0
|
45 |
RSemaphore waiting[2];
|
sl@0
|
46 |
|
sl@0
|
47 |
void over(TInt n)
|
sl@0
|
48 |
{
|
sl@0
|
49 |
// RDebug::Print(_L("Over(%d)"),n);
|
sl@0
|
50 |
waiting[1-n].Signal();
|
sl@0
|
51 |
waiting[n].Wait();
|
sl@0
|
52 |
}
|
sl@0
|
53 |
|
sl@0
|
54 |
#define THREAD0 if (thread==0)
|
sl@0
|
55 |
#define THREAD1 if (thread==(TAny*)1)
|
sl@0
|
56 |
#define OVER over((TInt)thread)
|
sl@0
|
57 |
|
sl@0
|
58 |
// Shared variables
|
sl@0
|
59 |
|
sl@0
|
60 |
int fd;
|
sl@0
|
61 |
|
sl@0
|
62 |
/**
|
sl@0
|
63 |
Interleaved test code
|
sl@0
|
64 |
|
sl@0
|
65 |
@SYMTestCaseID SYSLIB-STDLIB-CT-1071
|
sl@0
|
66 |
@SYMTestCaseDesc Tests for multi-threaded file descriptors
|
sl@0
|
67 |
@SYMTestPriority High
|
sl@0
|
68 |
@SYMTestActions Run threads to open,read,close test files
|
sl@0
|
69 |
@SYMTestExpectedResults Test must not fail
|
sl@0
|
70 |
@SYMREQ REQ0000
|
sl@0
|
71 |
*/
|
sl@0
|
72 |
TInt testfunction(TAny* thread)
|
sl@0
|
73 |
{
|
sl@0
|
74 |
int n;
|
sl@0
|
75 |
char *p;
|
sl@0
|
76 |
ttest_Next("Entering testfunction");
|
sl@0
|
77 |
//
|
sl@0
|
78 |
ttest_Next("Competing console reads - press 'A' then 'B'...");
|
sl@0
|
79 |
fflush(stdout);
|
sl@0
|
80 |
int c=getchar();
|
sl@0
|
81 |
fprintf(stderr, "t%d: read char %d\r\n", thread, c);
|
sl@0
|
82 |
//
|
sl@0
|
83 |
THREAD0
|
sl@0
|
84 |
{
|
sl@0
|
85 |
waiting[0].Wait(); // until Thread1 says OVER
|
sl@0
|
86 |
ttest_Next("Create test file");
|
sl@0
|
87 |
fd=open("c:\\testfile", O_RDWR+O_CREAT+O_TRUNC, 0777);
|
sl@0
|
88 |
test_ok(fd>=0);
|
sl@0
|
89 |
}
|
sl@0
|
90 |
//
|
sl@0
|
91 |
OVER;
|
sl@0
|
92 |
ttest_Next("Get the sequencing sorted out...");
|
sl@0
|
93 |
OVER;
|
sl@0
|
94 |
THREAD1
|
sl@0
|
95 |
{
|
sl@0
|
96 |
ttest_Next("Write to test file");
|
sl@0
|
97 |
p="Hello from thread 1\r\n";
|
sl@0
|
98 |
n=write(fd,p,strlen(p));
|
sl@0
|
99 |
test_ok(n==strlen(p));
|
sl@0
|
100 |
}
|
sl@0
|
101 |
THREAD0
|
sl@0
|
102 |
{
|
sl@0
|
103 |
ttest_Next("Close test file");
|
sl@0
|
104 |
close(fd);
|
sl@0
|
105 |
}
|
sl@0
|
106 |
OVER;
|
sl@0
|
107 |
THREAD1
|
sl@0
|
108 |
{
|
sl@0
|
109 |
ttest_Next("Reopen test file");
|
sl@0
|
110 |
fd=open("c:\\testfile",O_RDONLY,0);
|
sl@0
|
111 |
test_ok(fd>=0);
|
sl@0
|
112 |
}
|
sl@0
|
113 |
THREAD0
|
sl@0
|
114 |
{
|
sl@0
|
115 |
ttest_Next("Read from test file");
|
sl@0
|
116 |
char buf[80];
|
sl@0
|
117 |
buf[6]='\0';
|
sl@0
|
118 |
n=read(fd,buf,6);
|
sl@0
|
119 |
test_ok(n==6);
|
sl@0
|
120 |
test(strncmp(buf,"Hello ",6)==0);
|
sl@0
|
121 |
printf("Read >%s<... \r\n",buf);
|
sl@0
|
122 |
fflush(stdout);
|
sl@0
|
123 |
}
|
sl@0
|
124 |
OVER;
|
sl@0
|
125 |
THREAD1
|
sl@0
|
126 |
{
|
sl@0
|
127 |
ttest_Next("Read from test file");
|
sl@0
|
128 |
close(0);
|
sl@0
|
129 |
n=dup2(fd,0);
|
sl@0
|
130 |
test(n>=0); // associate stdin with "testfile"
|
sl@0
|
131 |
char buf[80];
|
sl@0
|
132 |
p=fgets(buf,80,stdin);
|
sl@0
|
133 |
test_ok(p==buf);
|
sl@0
|
134 |
fprintf(stderr, "Read >%s<\r\n", buf);
|
sl@0
|
135 |
}
|
sl@0
|
136 |
THREAD0
|
sl@0
|
137 |
{
|
sl@0
|
138 |
ttest_Next("Close test file");
|
sl@0
|
139 |
close(fd);
|
sl@0
|
140 |
}
|
sl@0
|
141 |
OVER;
|
sl@0
|
142 |
ttest_Next("Completed testfunction");
|
sl@0
|
143 |
waiting[0].Signal(); // allow thread0 to continue
|
sl@0
|
144 |
waiting[1].Signal(); // allow thread1 to continue
|
sl@0
|
145 |
return 0;
|
sl@0
|
146 |
}
|
sl@0
|
147 |
|
sl@0
|
148 |
// Thread management - main thread is thread0
|
sl@0
|
149 |
|
sl@0
|
150 |
void init_threads()
|
sl@0
|
151 |
{
|
sl@0
|
152 |
waiting[0].CreateLocal(0);
|
sl@0
|
153 |
waiting[1].CreateLocal(0);
|
sl@0
|
154 |
|
sl@0
|
155 |
RThread thread1;
|
sl@0
|
156 |
TInt err=thread1.Create(_L("Thread1"),testfunction,0x10000,NULL,(TAny*)1);
|
sl@0
|
157 |
test(err==KErrNone);
|
sl@0
|
158 |
test_Next("Starting thread1");
|
sl@0
|
159 |
thread1.Resume();
|
sl@0
|
160 |
test_Next("entering main test...");
|
sl@0
|
161 |
}
|
sl@0
|
162 |
|
sl@0
|
163 |
int main(int argc, char *argv[])
|
sl@0
|
164 |
{
|
sl@0
|
165 |
// SpawnPosixServerThread(); - provided by MCRT0.OBJ
|
sl@0
|
166 |
|
sl@0
|
167 |
start_redirection_server();
|
sl@0
|
168 |
|
sl@0
|
169 |
test_Title("TMTHREAD");
|
sl@0
|
170 |
init_threads();
|
sl@0
|
171 |
testfunction(0);
|
sl@0
|
172 |
test_Close();
|
sl@0
|
173 |
return KErrNone;
|
sl@0
|
174 |
}
|