Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src-test / org / gvsig / fmap / dal / resource / BlockTest.java @ 25940

History | View | Annotate | Download (7.14 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I. S.A.   {{Task}}
26
*/
27

    
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.dal.resource;
32

    
33
import java.util.Random;
34

    
35
import junit.framework.TestCase;
36

    
37
/**
38
 * @author jmvivo
39
 *
40
 */
41
public class BlockTest extends TestCase {
42

    
43
        static int MAX_TIME_OPEN = 200;
44

    
45
        /* (non-Javadoc)
46
         * @see junit.framework.TestCase#setUp()
47
         */
48
        protected void setUp() throws Exception {
49
                super.setUp();
50
        }
51

    
52
        public void testBlocker1() {
53
                AbstractBlocker blocker = new Blocker1();
54

    
55
                runTesters(blocker, 5, 6);
56
                runMultyTesters(blocker, 5, 6, 3);
57
        }
58

    
59
        public void testBlocker2() {
60
                AbstractBlocker blocker = new Blocker2();
61

    
62
                runTesters(blocker, 5, 6);
63
                runMultyTesters(blocker, 5, 6, 3);
64

    
65
        }
66

    
67
        private void runTesters(AbstractBlocker blocker, int numTesters,
68
                        int maxBegins) {
69
                BlockerTester[] testers = new BlockerTester[numTesters];
70
                Random rnd = new Random();
71
                rnd.setSeed(System.currentTimeMillis());
72
                for (int i = 0; i < testers.length; i++) {
73
                        testers[i] = new BlockerTester(i, blocker,
74
                                        rnd.nextInt(maxBegins) + 1);
75
                }
76

    
77
                for (int i = 0; i < testers.length; i++) {
78
                        testers[i].start();
79
                }
80

    
81
                try {
82
                        Thread.sleep(
83
                                        (numTesters * maxBegins * MAX_TIME_OPEN) + (1000));
84
                } catch (InterruptedException e) {
85
                        // TODO Auto-generated catch block
86
                        e.printStackTrace();
87
                }
88

    
89
                for (int i = 0; i < testers.length; i++) {
90
                        assertTrue("Tester " + i, testers[i].isOk());
91
                }
92
        }
93

    
94
        private void runMultyTesters(AbstractBlocker blocker, int numTesters,
95
                        int maxBegins, int maxInternalBegins) {
96
                BlockerMultyTester[] testers = new BlockerMultyTester[numTesters];
97
                Random rnd = new Random();
98
                rnd.setSeed(System.currentTimeMillis());
99
                for (int i = 0; i < testers.length; i++) {
100
                        testers[i] = new BlockerMultyTester(i, blocker,
101
                                        rnd
102
                                        .nextInt(maxBegins) + 1, rnd.nextInt(maxInternalBegins));
103
                }
104

    
105
                for (int i = 0; i < testers.length; i++) {
106
                        testers[i].start();
107
                }
108

    
109
                try {
110
                        Thread
111
                                        .sleep((numTesters * maxBegins * maxInternalBegins * MAX_TIME_OPEN) + (1000));
112
                } catch (InterruptedException e) {
113
                        // TODO Auto-generated catch block
114
                        e.printStackTrace();
115
                }
116

    
117
                for (int i = 0; i < testers.length; i++) {
118
                        assertTrue("Tester " + i, testers[i].isOk());
119
                }
120
        }
121

    
122

    
123
        class BlockerTester extends Thread {
124
                AbstractBlocker blocker;
125

    
126
                private boolean isOk;
127

    
128
                private int timesToDo;
129

    
130
                private int id;
131

    
132
                /**
133
                 *
134
                 */
135
                public BlockerTester(int id, AbstractBlocker blocker, int timesToDo) {
136
                        super();
137
                        this.id = id;
138
                        this.blocker = blocker;
139
                        this.timesToDo = timesToDo;
140
                        this.isOk = false;
141
                }
142

    
143
                private boolean checkBlocker(int data) {
144
                        if (!blocker.inUse()) {
145
                                System.out.println("a");
146
                                return false;
147
                        }
148
                        if (blocker.getConsumerId() != this.id) {
149
                                System.out.println("b");
150
                                return false;
151
                        }
152
                        if (blocker.getConsumerData() != data) {
153
                                System.out.println("c");
154
                                return false;
155
                        }
156

    
157
                        return true;
158
                }
159

    
160
                public void run() {
161
                        Random rnd = new Random();
162
                        rnd.setSeed(System.currentTimeMillis());
163

    
164
                        for (int i = 1; i <= this.timesToDo; i++) {
165
                                System.out.println("" + this.id + ": begin (" + i + "/"
166
                                                + this.timesToDo + ")");
167
                                this.blocker.begin(this.id, i);
168

    
169

    
170
                                System.out.println("" + this.id + ": runing (" + i + "/"
171
                                                + this.timesToDo + ")");
172

    
173
                                if (!this.checkBlocker(i)) {
174
                                        this.isOk = false;
175
                                        return;
176
                                }
177
                                try {
178
                                        sleep(rnd.nextInt(MAX_TIME_OPEN) + 1);
179
                                } catch (InterruptedException e) {
180
                                        throw new RuntimeException(e);
181
                                } finally {
182

    
183
                                        if (!this.checkBlocker(i)) {
184
                                                this.isOk = false;
185
                                                return;
186
                                        }
187

    
188
                                        System.out.println("" + this.id + ": end (" + i + "/"
189
                                                        + this.timesToDo + ")");
190

    
191
                                        this.blocker.end();
192
                                }
193
                        }
194
                        System.out.println("" + this.id + ": ok " + this.timesToDo
195
                                        + " times");
196

    
197
                        this.isOk = true;
198
                }
199

    
200
                public boolean isOk() {
201
                        return this.isOk;
202
                }
203
        }
204

    
205
        /**
206
         * 
207
         * 
208
         * @author jmvivo
209
         * 
210
         */
211
        class BlockerMultyTester extends Thread {
212
                AbstractBlocker blocker;
213

    
214
                private boolean isOk;
215

    
216
                private int timesToDo;
217

    
218
                private int timesToDoInternal;
219

    
220
                private int id;
221

    
222
                private Random rnd = new Random();
223

    
224
                /**
225
                 *
226
                 */
227
                public BlockerMultyTester(int id, AbstractBlocker blocker,
228
                                int timesToDo, int timesToDoInternal) {
229
                        super();
230
                        this.id = id;
231
                        this.blocker = blocker;
232
                        this.timesToDo = timesToDo;
233
                        this.timesToDoInternal = timesToDoInternal;
234
                        this.isOk = false;
235
                }
236

    
237
                private boolean checkBlocker(int data) {
238
                        if (!blocker.inUse()) {
239
                                System.out.println("a");
240
                                return false;
241
                        }
242
                        if (blocker.getConsumerId() != this.id) {
243
                                System.out.println("b");
244
                                return false;
245
                        }
246
                        if (blocker.getConsumerData() != data) {
247
                                System.out.println("c");
248
                                return false;
249
                        }
250

    
251
                        return true;
252
                }
253

    
254
                private void doEnd(int data, int internal) {
255
                        String idStr;
256
                        if (internal > 0) {
257
                                idStr = "(" + data + "/" + this.timesToDo + "." + internal
258
                                                + ")";
259
                        } else {
260
                                idStr = "(" + data + "/" + this.timesToDo + ")";
261
                        }
262

    
263
                        if (!this.checkBlocker(data)) {
264
                                this.isOk = false;
265
                                return;
266
                        }
267

    
268
                        System.out.println("" + this.id + ": end " + idStr);
269

    
270
                        this.blocker.end();
271

    
272
                }
273

    
274
                private void doBegin(int data, int internal) {
275
                        String idStr;
276
                        if (internal > 0) {
277
                                idStr = "(" + data + "/" + this.timesToDo + "." + internal
278
                                                + ")";
279
                        } else {
280
                                idStr = "(" + data + "/" + this.timesToDo + ")";
281
                        }
282
                        System.out.println("" + this.id + ": begin " +idStr);
283
                        this.blocker.begin(this.id, data);
284

    
285
                        System.out.println("" + this.id + ": runing " + idStr);
286

    
287
                        if (!this.checkBlocker(data)) {
288
                                this.isOk = false;
289
                                return;
290
                        }
291

    
292
                }
293

    
294
                public void run() {
295
                        rnd.setSeed(System.currentTimeMillis());
296

    
297
                        int count = 0;
298

    
299
                        for (int i = 1; i <= this.timesToDo; i++) {
300

    
301
                                for (int j = 0; j <= this.timesToDoInternal; j++) {
302

    
303
                                        this.doBegin(i, j);
304
                                        try {
305
                                                sleep(rnd.nextInt(MAX_TIME_OPEN) + 1);
306
                                        } catch (InterruptedException e) {
307
                                                throw new RuntimeException(e);
308
                                        }
309
                                        count ++;
310

    
311
                                }
312

    
313
                                for (int j = 0; j <= this.timesToDoInternal; j++) {
314

    
315
                                        this.doEnd(i, this.timesToDoInternal - j);
316
                                        try {
317
                                                sleep(rnd.nextInt(MAX_TIME_OPEN) + 1);
318
                                        } catch (InterruptedException e) {
319
                                                throw new RuntimeException(e);
320
                                        }
321

    
322
                                }
323
                        }
324
                        System.out.println("" + this.id + ": ok " + count
325
                                        + " times");
326
                        this.isOk = true;
327
                }
328

    
329
                public boolean isOk() {
330
                        return this.isOk;
331
                }
332
        }
333

    
334
}
335