/usr/share/cruisecontrol-bin-2.6.1/projects/qpid-trunk/cpp/src/tests/MockConnectionInputHandler.h

00001 #ifndef _tests_MockConnectionInputHandler_h
00002 #define _tests_MockConnectionInputHandler_h
00003 
00004 /*
00005  *
00006  * Copyright (c) 2006 The Apache Software Foundation
00007  *
00008  * Licensed under the Apache License, Version 2.0 (the "License");
00009  * you may not use this file except in compliance with the License.
00010  * You may obtain a copy of the License at
00011  *
00012  *    http://www.apache.org/licenses/LICENSE-2.0
00013  *
00014  * Unless required by applicable law or agreed to in writing, software
00015  * distributed under the License is distributed on an "AS IS" BASIS,
00016  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017  * See the License for the specific language governing permissions and
00018  * limitations under the License.
00019  *
00020  */
00021 
00022 #include "qpid/sys/ConnectionInputHandler.h"
00023 #include "qpid/sys/ConnectionInputHandlerFactory.h"
00024 #include "qpid/sys/Monitor.h"
00025 
00026 struct MockConnectionInputHandler : public qpid::sys::ConnectionInputHandler {
00027 
00028     MockConnectionInputHandler() : state(START) {}
00029 
00030     ~MockConnectionInputHandler() {}
00031     
00032     void received(qpid::framing::AMQFrame* framep) {
00033         qpid::sys::Monitor::ScopedLock l(monitor);
00034         frame = *framep;
00035         setState(GOT_FRAME);
00036     }
00037 
00038     qpid::framing::AMQFrame waitForFrame() {        
00039         waitFor(GOT_FRAME);
00040         return frame;
00041     }
00042 
00043     void waitForClosed() {
00044         waitFor(CLOSED);
00045     }
00046     
00047     void closed() {
00048         qpid::sys::Monitor::ScopedLock l(monitor);
00049         setState(CLOSED);
00050     }
00051 
00052     void idleOut() {}
00053     void idleIn() {}
00054 
00055   private:
00056     typedef enum { START, GOT_FRAME, CLOSED } State;
00057 
00058     void setState(State s) {
00059         state = s;
00060         monitor.notify();
00061     }
00062     
00063     void waitFor(State s) {
00064         qpid::sys::Monitor::ScopedLock l(monitor);
00065         qpid::sys::Time deadline = qpid::sys::now() + 10*qpid::sys::TIME_SEC; 
00066         while (state != s)
00067             CPPUNIT_ASSERT(monitor.wait(deadline));
00068     }
00069 
00070     qpid::sys::Monitor  monitor;
00071     State state;
00072     qpid::framing::AMQFrame frame;
00073 };
00074 
00075 
00076 struct MockConnectionInputHandlerFactory : public qpid::sys::ConnectionInputHandlerFactory {
00077     MockConnectionInputHandlerFactory() : handler(0) {}
00078 
00079     qpid::sys::ConnectionInputHandler* create(qpid::sys::ConnectionOutputHandler*) {
00080         qpid::sys::Monitor::ScopedLock lock(monitor);
00081         handler = new MockConnectionInputHandler();
00082         monitor.notifyAll();
00083         return handler;
00084     }
00085 
00086     void waitForHandler() {
00087         qpid::sys::Monitor::ScopedLock lock(monitor);
00088         qpid::sys::Time deadline =
00089             qpid::sys::now() + 500 * qpid::sys::TIME_SEC;
00090         while (handler == 0)
00091             CPPUNIT_ASSERT(monitor.wait(deadline));
00092     }
00093     
00094     MockConnectionInputHandler* handler;
00095     qpid::sys::Monitor monitor;
00096 };
00097 
00098 
00099 
00100 #endif  

Generated on Thu Apr 10 11:08:18 2008 for Qpid by  doxygen 1.4.7