Module: Stub

Includes:
Env
Included in:
MCBasePatternHelper, StubTest
Defined in:
lib/common/socket/stub.rb

Overview

下游桩相关的服务

Instance Method Summary collapse

Methods included from Env

#bak_conf, #copy_conf, #get_conf, #get_thread_num, #is_process_running?, #kill_process, #modify_conf, #modify_conf_single, #recover_conf, #start_process

Instance Method Details

#set_stub_action(phash) ⇒ Object

功能

设置下游桩的工作模式

参数

  • phash: 参数hash

Example

set_stub_action ‘stub1’=>1, ‘stub2’=>2 set_stub_action ‘stub2’=>2



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/common/socket/stub.rb', line 88

def set_stub_action phash
	phash.each_pair{|key, value|
		#get idx
		idx = get_idx_by_name key, Context.get(:STUBS)
		
		#modify context according to the idx
		actions = Context.get :STUB_ACTIONS
		actions[idx] = value
		Context.set :STUB_ACTIONS, actions
	}

	save_actions
end

#set_stub_file(phash) ⇒ Object

功能

设置下游桩的响应文件

参数

  • phash: 参数hash

Example

set_stub_file ‘stub1’=>‘/home/…/data1’, ‘stub2’=>‘/home/…/data2’ set_stub_file ‘stub1’=>‘/home/…/data1’



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/common/socket/stub.rb', line 134

def set_stub_file phash
	phash.each_pair{|key, value|
                       #get idx
                       idx = get_idx_by_name key, Context.get(:STUBS)

                       #modify context according to the idx
                       files = Context.get :STUB_FILES
                       files[idx] = value
                       Context.set :STUB_FILES, files
               }

               save_files
end

#set_stub_head(phash) ⇒ Object

功能

设置下游桩的响应头

参数

  • phash: 参数hash

Example

set_stub_file ‘stub1’=>‘/home/…/head1’, ‘stub2’=>‘/home/…/head2’ set_stub_file ‘stub1’=>‘/home/…/head1’



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/common/socket/stub.rb', line 111

def set_stub_head phash
	phash.each_pair{|key, value|
                       #get idx
                       idx = get_idx_by_name key, Context.get(:STUBS)

                       #modify context according to the idx
                       heads = Context.get :STUB_HEADS
                       heads[idx] = value
                       Context.set :STUB_HEADS, heads
               }

               save_heads
end

#start_stub(phash) ⇒ Object

功能

启动1到多个下游桩

参数

  • phash: 参数hash

  • return:如果启动失败,抛出异常

Example

start_stub ‘stub1’=>9998, ‘stub2’=>9999



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/common/socket/stub.rb', line 28

def start_stub phash
	clear_context

	#load into mem
	stubs = []
	ports = []
	actions = []
	heads = []
	files = []
	phash.each_pair{|key, value|
		stubs << key
		ports << value

		#by default
		actions << 0
		heads << ""
		files << ""
	}

	Context.set :STUBS, stubs
	Context.set :STUB_PORTS, ports
	Context.set :STUB_ACTIONS, actions
	Context.set :STUB_HEADS, heads
	Context.set :STUB_FILES, files
	

	#modify server.conf
	save_to_conf

	#start server
	#cmd_str = "cd #{STUB_PATH}; nohup ts-agent -f server.tcl >/dev/null 2>&1 &"
	cmd_str = "cd #{STUB_PATH}; nohup ts-agent -f server.tcl  &"
	system "#{cmd_str}"
	
	sleep 1

	#TODO get it's background pid?
end

#stop_stubObject

功能

停止前一次启动的下游桩,需要上下文支持

example

stop_stub



71
72
73
74
75
76
77
# File 'lib/common/socket/stub.rb', line 71

def stop_stub
	`killall -9 ts-agent`
	clear_context

	sleep 1
	#TODO kill just by pid
end