@@ -33,11 +33,63 @@ class MockPthreadsNull;
3333
3434using namespace std ;
3535
36+ #ifdef THREADED
37+ class CheckedPthreadCreateFailure : public CheckedPthread
38+ {
39+ public:
40+ explicit CheckedPthreadCreateFailure (int failOnCall):
41+ pthreadCreateCounter(0 ),pthreadJoinCounter(0 ),
42+ pthreadCondDestroyCounter(0 ),pthreadMutexDestroyCounter(0 ),
43+ threadCreated(false ),failOnCall_(failOnCall){}
44+
45+ int pthread_create (pthread_t *t, const pthread_attr_t *a,
46+ void *(*f)(void *), void *d) override {
47+ if (pthreadCreateCounter++ == failOnCall_)
48+ return EAGAIN ;
49+ int rc=CheckedPthread::pthread_create (t,a,f,d);
50+ if (!rc) {
51+ thread=*t;
52+ threadCreated=true ;
53+ }
54+ return rc;
55+ }
56+
57+ int pthread_join (pthread_t t, void **r) override {
58+ pthreadJoinCounter++;
59+ return CheckedPthread::pthread_join (t,r);
60+ }
61+
62+ int pthread_cond_destroy (pthread_cond_t *c) override {
63+ pthreadCondDestroyCounter++;
64+ return CheckedPthread::pthread_cond_destroy (c);
65+ }
66+
67+ int pthread_mutex_destroy (pthread_mutex_t *m) override {
68+ pthreadMutexDestroyCounter++;
69+ return CheckedPthread::pthread_mutex_destroy (m);
70+ }
71+
72+ int pthreadCreateCounter;
73+ int pthreadJoinCounter;
74+ int pthreadCondDestroyCounter;
75+ int pthreadMutexDestroyCounter;
76+ pthread_t thread;
77+ bool threadCreated;
78+
79+ private:
80+ int failOnCall_;
81+ };
82+ #endif
83+
3684class Zookeeper_init : public CPPUNIT_NS ::TestFixture
3785{
3886 CPPUNIT_TEST_SUITE (Zookeeper_init);
3987 CPPUNIT_TEST (testVersion);
4088 CPPUNIT_TEST (testBasic);
89+ #ifdef THREADED
90+ CPPUNIT_TEST (testFirstThreadCreateFailure);
91+ CPPUNIT_TEST (testSecondThreadCreateFailure);
92+ #endif
4193 CPPUNIT_TEST (testAddressResolution);
4294 CPPUNIT_TEST (testMultipleAddressResolution);
4395 CPPUNIT_TEST (testNullAddressString);
@@ -139,6 +191,36 @@ class Zookeeper_init : public CPPUNIT_NS::TestFixture
139191 CPPUNIT_ASSERT (MockPthreadsNull::isInitialized (&zh->completions_to_process .cond ));
140192#endif
141193 }
194+ #ifdef THREADED
195+ void assertThreadCreateFailure (int failOnCall)
196+ {
197+ delete pthreadMock;
198+ pthreadMock=0 ;
199+ CheckedPthreadCreateFailure pthreadFailure (failOnCall);
200+
201+ errno=0 ;
202+ zh=zookeeper_init (" 127.0.0.1:2121" ,watcher,10000 ,0 ,0 ,0 );
203+
204+ CPPUNIT_ASSERT (zh==0 );
205+ CPPUNIT_ASSERT_EQUAL (EAGAIN ,errno);
206+ CPPUNIT_ASSERT_EQUAL (failOnCall+1 ,pthreadFailure.pthreadCreateCounter );
207+ CPPUNIT_ASSERT_EQUAL (failOnCall,pthreadFailure.pthreadJoinCounter );
208+ CPPUNIT_ASSERT_EQUAL (3 ,pthreadFailure.pthreadCondDestroyCounter );
209+ CPPUNIT_ASSERT_EQUAL (9 ,pthreadFailure.pthreadMutexDestroyCounter );
210+ if (pthreadFailure.threadCreated )
211+ CPPUNIT_ASSERT (CheckedPthread::isDestroyed (pthreadFailure.thread ));
212+ }
213+
214+ void testFirstThreadCreateFailure ()
215+ {
216+ assertThreadCreateFailure (0 );
217+ }
218+
219+ void testSecondThreadCreateFailure ()
220+ {
221+ assertThreadCreateFailure (1 );
222+ }
223+ #endif
142224 void testAddressResolution ()
143225 {
144226 const char EXPECTED_IPS [][4 ]={{127 ,0 ,0 ,1 }};
0 commit comments