MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
yassl-t.cc
1 /* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 // First include (the generated) my_config.h, to get correct platform defines.
17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 
20 #ifdef __WIN__
21 #include<Windows.h>
22 #else
23 #include <pthread.h>
24 #endif
25 
26 #include "runtime.hpp"
27 #include "yassl_int.hpp"
28 
29 #include "thread_utils.h"
30 
31 namespace {
32 
34 using thread::Thread;
35 
36 class Yassl_thread : public Thread
37 {
38 public:
39  Yassl_thread(Notification *go, Notification *done)
40  : m_sessions_instance(NULL), m_go(go), m_done(done)
41  {}
42  virtual void run()
43  {
44  // Wait until my creator tells me to go.
45  m_go->wait_for_notification();
46  yaSSL::Sessions &sessions= yaSSL::GetSessions();
47  m_sessions_instance= &sessions;
48  // Tell my creator I'm done.
49  m_done->notify();
50  }
51  yaSSL::Sessions *m_sessions_instance;
52 private:
53  Notification *m_go;
54  Notification *m_done;
55 };
56 
57 
66 TEST(YasslTest, ManySessions)
67 {
68  Notification go[5];
69  Notification done[5];
70  Yassl_thread t0(&go[0], &done[0]);
71  Yassl_thread t1(&go[1], &done[1]);
72  Yassl_thread t2(&go[2], &done[2]);
73  Yassl_thread t3(&go[3], &done[3]);
74  Yassl_thread t4(&go[4], &done[4]);
75 
76  t0.start();
77  t1.start();
78  t2.start();
79  t3.start();
80  t4.start();
81 
82  for (int ix= 0; ix < 5; ++ix)
83  go[ix].notify();
84 
85  for (int ix= 0; ix < 5; ++ix)
86  done[ix].wait_for_notification();
87 
88  // These are most likely to fail unless we use pthread_once.
89  EXPECT_EQ(t0.m_sessions_instance, t1.m_sessions_instance);
90  EXPECT_EQ(t0.m_sessions_instance, t2.m_sessions_instance);
91  EXPECT_EQ(t0.m_sessions_instance, t3.m_sessions_instance);
92  EXPECT_EQ(t0.m_sessions_instance, t4.m_sessions_instance);
93 
94  // These rarely fail. If they do, we have more than two instances.
95  EXPECT_EQ(t1.m_sessions_instance, t2.m_sessions_instance);
96  EXPECT_EQ(t1.m_sessions_instance, t3.m_sessions_instance);
97  EXPECT_EQ(t1.m_sessions_instance, t4.m_sessions_instance);
98 
99  EXPECT_EQ(t2.m_sessions_instance, t3.m_sessions_instance);
100  EXPECT_EQ(t2.m_sessions_instance, t4.m_sessions_instance);
101 
102  EXPECT_EQ(t3.m_sessions_instance, t4.m_sessions_instance);
103 
104  t0.join();
105  t1.join();
106  t2.join();
107  t3.join();
108  t4.join();
109  yaSSL_CleanUp();
110 }
111 
112 }