MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_cpcd.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 
20 #include <ndb_global.h>
21 #include "../CpcClient.hpp"
22 #include <Vector.hpp>
23 
24 SimpleCpcClient g_client("localhost", 1234);
26 
27 void define();
28 void start(SimpleCpcClient::Process & p);
29 void stop(SimpleCpcClient::Process & p);
30 void undefine(SimpleCpcClient::Process & p);
31 void list();
32 SimpleCpcClient::Process* find(int id);
33 
34 #define ABORT() {ndbout_c("ABORT"); while(true); abort();}
35 
36 int name = 0;
37 
38 int
39 main(void){
40 
41  g_client.connect();
42 
43  srand(time(0));
44  for(int i = 0; i<1000; i++){
45  int sz = g_procs.size();
46  int test = rand() % 100;
47  if(sz == 0 || test < 10){
48  define();
49  continue;
50  }
51 
52  list();
53 
54  int proc = rand() % g_procs.size();
55  SimpleCpcClient::Process & p = g_procs[proc];
56  if(p.m_status == "running" && test > 50){
57  ndbout_c("undefine %d: %s (running)", p.m_id, p.m_name.c_str());
58  undefine(p);
59  g_procs.erase(proc);
60  continue;
61  }
62  if(p.m_status == "running" && test <= 50){
63  ndbout_c("stop %d: %s(running)", p.m_id, p.m_name.c_str());
64  stop(p);
65  continue;
66  }
67  if(p.m_status == "stopped" && test > 50){
68  ndbout_c("undefine %d: %s(stopped)", p.m_id, p.m_name.c_str());
69  undefine(p);
70  g_procs.erase(proc);
71  continue;
72  }
73  if(p.m_status == "stopped" && test <= 50){
74  ndbout_c("start %d %s(stopped)", p.m_id, p.m_name.c_str());
75  start(p);
76  continue;
77  }
78  ndbout_c("Unknown: %s", p.m_status.c_str());
79  }
80 }
81 
82 void define(){
84  m_proc.m_id = -1;
85  m_proc.m_type = "temporary";
86  m_proc.m_owner = "atrt";
87  m_proc.m_group = "group";
88  //m_proc.m_cwd
89  //m_proc.m_env
90  //proc.m_proc.m_stdout = "log.out";
91  //proc.m_proc.m_stderr = "2>&1";
92  //proc.m_proc.m_runas = proc.m_host->m_user;
93  m_proc.m_ulimit = "c:unlimited";
94  if((rand() & 15) >= 0){
95  m_proc.m_name.assfmt("%d-%d-%s", getpid(), name++, "sleep");
96  m_proc.m_path.assign("/bin/sleep");
97  m_proc.m_args = "600";
98  } else {
99  m_proc.m_name.assfmt("%d-%d-%s", getpid(), name++, "test.sh");
100  m_proc.m_path.assign("/home/jonas/run/cpcd/test.sh");
101  m_proc.m_args = "600";
102  }
103  g_procs.push_back(m_proc);
104 
106  if(g_client.define_process(g_procs.back(), reply) != 0){
107  ndbout_c("define %s -> ERR", m_proc.m_name.c_str());
108  reply.print();
109  ABORT();
110  }
111  ndbout_c("define %s -> %d", m_proc.m_name.c_str(), m_proc.m_id);
112 }
113 
114 void start(SimpleCpcClient::Process & p){
115  Properties reply;
116  if(g_client.start_process(p.m_id, reply) != 0){
117  reply.print();
118  ABORT();
119  }
120 }
121 
122 void stop(SimpleCpcClient::Process & p){
123  Properties reply;
124  if(g_client.stop_process(p.m_id, reply) != 0){
125  reply.print();
126  ABORT();
127  }
128 }
129 
130 void undefine(SimpleCpcClient::Process & p){
131  Properties reply;
132  if(g_client.undefine_process(p.m_id, reply) != 0){
133  reply.print();
134  ABORT();
135  }
136 }
137 
138 void list(){
139  Properties reply;
141  if(g_client.list_processes(procs, reply) != 0){
142  reply.print();
143  ABORT();
144  }
145 
146  for(Uint32 i = 0; i<procs.size(); i++){
147  SimpleCpcClient::Process * p = find(procs[i].m_id);
148  if(p != 0){
149  p->m_status = procs[i].m_status;
150  }
151  }
152 }
153 SimpleCpcClient::Process* find(int id){
154  for(Uint32 i = 0; i<g_procs.size(); i++){
155  if(g_procs[i].m_id == id)
156  return &g_procs[i];
157  }
158  return 0;
159 }