summaryrefslogtreecommitdiff
path: root/mysql-test/t/kill_n_check.sh
blob: 96c402a638cff68502f6a2f4f1dc50e3e7fcec9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh

###########################################################################

# NOTE: this script returns 0 (success) even in case of failure (except for
# usage-error). This is because this script is executed under
# mysql-test-run[.pl] and it's better to examine particular problem in log
# file, than just having said that the test case has failed.

###########################################################################

basename=`basename "$0"`
dirname=`dirname "$0"`

###########################################################################

. "$dirname/utils.sh"

###########################################################################

check_restart()
{
  if [ ! -r "$pid_path" ]; then
    log_debug "No '$pid_path' found."
    user_msg='the process was killed'
    return 1
  fi

  new_pid=`cat "$pid_path" 2>/dev/null`
  err_code=$?

  log_debug "err_code: $err_code; original_pid: $original_pid; new_pid: $new_pid."

  if [ $err_code -eq 0 -a "$original_pid" = "$new_pid" ]; then
    log_debug "The process was not restarted."
    user_msg='the process was not restarted'
    return 1
  fi

  log_debug "The process was restarted."
  user_msg='the process was restarted'
  return 0
}

###########################################################################

if [ $# -ne 4 ]; then
  echo "Usage: $basename <pid file path> killed|restarted <timeout> <test id>"
  exit 1
fi

pid_path="$1"
expected_result="$2"
total_timeout="$3"
test_id="$4"
log_file="$MYSQLTEST_VARDIR/log/$test_id.log"

log_debug "-- $basename: starting --"
log_debug "pid_path: '$pid_path'"
log_debug "expected_result: '$expected_result'"
log_debug "total_timeout: '$total_timeout'"
log_debug "test_id: '$test_id'"
log_debug "log_file: '$log_file'"

###########################################################################

if [ "$expected_result" != 'killed' -a \
     "$expected_result" != 'restarted' ]; then
  log_error "Invalid second argument ($expected_result): 'killed' or 'restarted' expected."
  quit 0
fi

if [ -z "$pid_path" ]; then
  log_error "Invalid PID path ($pid_path)."
  quit 0
fi

if [ ! -r "$pid_path" ]; then
  log_error "PID file ($pid_path) does not exist."
  quit 0
fi

if [ -z "$total_timeout" ]; then
  log_error "Timeout is not specified."
  quit 0
fi

###########################################################################

original_pid=`cat "$pid_path"`
log_debug "original_pid: $original_pid."

log_info "Killing the process..."

kill -9 $original_pid

###########################################################################

log_info "Waiting..."

if [ "$expected_result" = "restarted" ]; then

  # Wait for the process to restart.

  cur_attempt=1

  while true; do

    log_debug "cur_attempt: $cur_attempt."

    if check_restart; then
      log_info "Success: $user_msg."
      quit 0
    fi

    [ $cur_attempt -ge $total_timeout ] && break

    log_debug "Sleeping for 1 second..."
    sleep 1

    cur_attempt=`expr $cur_attempt + 1`

  done

  log_error "$user_msg."
  quit 0

else # $expected_result == killed

  # Here we have to sleep for some long time to ensure that the process will
  # not be restarted.

  log_debug "Sleeping for $total_timeout seconds..."
  sleep $total_timeout

  new_pid=`cat "$pid_path" 2>/dev/null`
  log_debug "new_pid: $new_pid."

  if [ "$new_pid" -a "$new_pid" -ne "$original_pid" ]; then
    log_error "The process was restarted."
  else
    log_info "Success: the process was killed."
  fi

  quit 0

fi