/[resiprocate]/main/rutil/test/testFifo.cxx
ViewVC logotype

Diff of /main/rutil/test/testFifo.cxx

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4599 by derek, Wed May 11 23:10:19 2005 UTC revision 4746 by dlb, Wed May 25 01:02:32 2005 UTC
# Line 5  Line 5 
5  #include "resiprocate/os/TimeLimitFifo.hxx"  #include "resiprocate/os/TimeLimitFifo.hxx"
6  #include "resiprocate/os/Data.hxx"  #include "resiprocate/os/Data.hxx"
7  #include "resiprocate/os/ThreadIf.hxx"  #include "resiprocate/os/ThreadIf.hxx"
8    #include "resiprocate/os/Timer.hxx"
9  #include <unistd.h>  #include <unistd.h>
10    
11  #define VERBOSE  //#define VERBOSE
12    
13  using namespace resip;  using namespace resip;
14  using namespace std;  using namespace std;
# Line 26  Line 27 
27  {  {
28    public:    public:
29      Consumer(TimeLimitFifo<Foo>&);      Consumer(TimeLimitFifo<Foo>&);
30      ~Consumer() {};        virtual ~Consumer()
31          {
32             shutdown();
33             join();
34    #ifdef VERBOSE
35             cerr << "Consumer thread finished" << endl;
36    #endif
37    
38             shutdown();
39             join();
40          };
41    
42      void thread();      void thread();
43    
# Line 38  Line 49 
49  {  {
50    public:    public:
51      Producer(TimeLimitFifo<Foo>&);      Producer(TimeLimitFifo<Foo>&);
52      ~Producer() {};        virtual ~Producer()
53          {
54             shutdown();
55             join();
56    #ifdef VERBOSE
57             cerr << "Producer thread finished" << endl;
58    #endif
59    
60             shutdown();
61             join();
62          }
63    
64      void thread();      void thread();
65    
# Line 52  Line 73 
73    
74  void Consumer::thread()  void Consumer::thread()
75  {  {
76      static unsigned wakeups[6] = { 1, 2, 3, 0, 1, 3 };      static unsigned wakeups[6] = { 10000, 20000, 30000, 0, 10000, 30000 };
77      unsigned int w = 0;      unsigned int w = 0;
78    
79      while(!mShutdown) {      while (!mShutdown)
80        {
81          if (mFifo.messageAvailable())          if (mFifo.messageAvailable())
82              mFifo.getNext();         {
83              mFifo.getNext(100);
84           }
85          else          else
86          {          {
87              unsigned wakeup = wakeups[w];              unsigned wakeup = wakeups[w];
88              w = (w + 1) % 6;              w = (w + 1) % 6;
89  #ifdef VERBOSE  #ifdef VERBOSE
90              cerr << "Consumer sleeping for " << wakeup << " seconds with mSize " << mFifo.size() << endl;            cerr << "Consumer sleeping for " << wakeup << " useconds with mSize " << mFifo.size() << endl;
91  #endif  #endif
92              if (wakeup > 0)              if (wakeup > 0)
93                  sleep(wakeup);            {
94                 usleep(wakeup);
95              }
96          }          }
97      }      }
98  }  }
# Line 77  Line 103 
103    
104  void Producer::thread()  void Producer::thread()
105  {  {
106      static unsigned wakeups[6] = { 0, 1, 0, 2, 3, 1 };     static unsigned wakeups[6] = { 0, 10000, 0, 20000, 30000, 10000 };
107      unsigned int w = 0;      unsigned int w = 0;
108    
109      for (unsigned long n = 0; n < 0x1ffff; n++) {     for (unsigned long n = 0; n < 0x1ffff; n++)
110       {
111          if (mFifo.wouldAccept(TimeLimitFifo<Foo>::EnforceTimeDepth))          if (mFifo.wouldAccept(TimeLimitFifo<Foo>::EnforceTimeDepth))
112          {
113              mFifo.add(new Foo(Data(n)), TimeLimitFifo<Foo>::EnforceTimeDepth);              mFifo.add(new Foo(Data(n)), TimeLimitFifo<Foo>::EnforceTimeDepth);
114          }
115          else          else
116          {          {
117              unsigned wakeup = wakeups[w];              unsigned wakeup = wakeups[w];
118              w = (w + 1) % 6;              w = (w + 1) % 6;
119  #ifdef VERBOSE  #ifdef VERBOSE
120              cerr << "Producer sleeping for " << wakeup << " seconds at " << n << " with mSize " << mFifo.size() << endl;           cerr << "Producer sleeping for " << wakeup << " useconds at " << n << " with mSize " << mFifo.size() << endl;
121  #endif  #endif
122              if (wakeup > 0)              if (wakeup > 0)
123                  sleep(wakeup);           {
124                usleep(wakeup);
125             }
126          }          }
127      }      }
128  }  }
# Line 111  Line 142 
142     Fifo<Foo> f;     Fifo<Foo> f;
143     FiniteFifo<Foo> ff(5);     FiniteFifo<Foo> ff(5);
144    
    cerr << "!! PreFirst" << endl;  
   
145     {     {
146          cerr << "!! test basic" << endl;
147          
148        bool c;        bool c;
149        TimeLimitFifo<Foo> tlf(5, 10); // 5 seconds or 10 count limit        TimeLimitFifo<Foo> tlf(5, 10); // 5 seconds or 10 count limit
150    
# Line 172  Line 203 
203     }     }
204    
205     {     {
206          cerr << "!! Test time depth" << endl;
207    
208        TimeLimitFifo<Foo> tlfNS(5, 0); // 5 seconds, no count limit        TimeLimitFifo<Foo> tlfNS(5, 0); // 5 seconds, no count limit
209        bool c;        bool c;
210    
# Line 227  Line 260 
260     }     }
261    
262     {     {
263          cerr << "!! Test reserved" << endl;
264    
265        TimeLimitFifo<Foo> tlfNS(5, 10); // 5 seconds, limit 10 (2 reserved)        TimeLimitFifo<Foo> tlfNS(5, 10); // 5 seconds, limit 10 (2 reserved)
266        bool c;        bool c;
267    
# Line 264  Line 299 
299     }     }
300    
301     {     {
302          cerr << "!! Test unlimited" << endl;
303    
304        TimeLimitFifo<Foo> tlfNS(0, 0); // unlimited        TimeLimitFifo<Foo> tlfNS(0, 0); // unlimited
305    
306        bool c;        bool c;
# Line 281  Line 318 
318     }     }
319        
320     {     {
321          cerr << "!! Test produce consumer" << endl;
322    
323         TimeLimitFifo<Foo> tlfNS(20, 5000);         TimeLimitFifo<Foo> tlfNS(20, 5000);
324         Producer prod(tlfNS);         Producer prod(tlfNS);
325         Consumer cons(tlfNS);         Consumer cons(tlfNS);
# Line 300  Line 339 
339         cerr << "Consumer thread finished" << endl;         cerr << "Consumer thread finished" << endl;
340  #endif  #endif
341     }     }
342    
343       {
344          cerr << "!! Test producers consumers" << endl;
345    
346           TimeLimitFifo<Foo> tlfNS(20, 50000);
347          
348           Producer prod1(tlfNS);
349           Producer prod2(tlfNS);
350           Producer prod3(tlfNS);
351           Producer prod4(tlfNS);
352           Producer prod5(tlfNS);
353           Producer prod6(tlfNS);
354           Producer prod7(tlfNS);
355           Producer prod8(tlfNS);
356           Producer prod9(tlfNS);
357           Producer prod10(tlfNS);
358    
359           Consumer cons1(tlfNS);
360           Consumer cons2(tlfNS);
361           Consumer cons3(tlfNS);
362           Consumer cons4(tlfNS);
363           Consumer cons5(tlfNS);
364           Consumer cons6(tlfNS);
365           Consumer cons7(tlfNS);
366           Consumer cons8(tlfNS);
367           Consumer cons9(tlfNS);
368           Consumer cons10(tlfNS);
369    
370    
371           cons1.run();
372           cons2.run();
373           cons3.run();
374           cons4.run();
375           cons5.run();
376           cons6.run();
377           cons7.run();
378           cons8.run();
379           cons9.run();
380           cons10.run();
381    
382           cerr << "before getNext(1000) " << Timer::getTimeMs() << endl;
383           tlfNS.getNext(1000);
384           cerr << "after getNext(1000) " << Timer::getTimeMs() << endl;
385    
386           prod1.run();
387    
388           cerr << "before getNext(1000) " << Timer::getTimeMs() << endl;
389           tlfNS.getNext(1000);
390           cerr << "after getNext(1000) " << Timer::getTimeMs() << endl;
391    
392           prod2.run();
393           prod3.run();
394           prod4.run();
395           prod5.run();
396           prod6.run();
397           prod7.run();
398           prod8.run();
399           prod9.run();
400           prod10.run();
401    
402       }
403    
404     cerr << "All OK" << endl;     cerr << "All OK" << endl;
405     return 0;     return 0;
406  }  }
# Line 307  Line 408 
408  /* ====================================================================  /* ====================================================================
409   * The Vovida Software License, Version 1.0   * The Vovida Software License, Version 1.0
410   *   *
  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.  
  *  
411   * Redistribution and use in source and binary forms, with or without   * Redistribution and use in source and binary forms, with or without
412   * modification, are permitted provided that the following conditions   * modification, are permitted provided that the following conditions
413   * are met:   * are met:

Legend:
Removed from v.4599  
changed lines
  Added in v.4746

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27