summaryrefslogtreecommitdiff
path: root/uvim/src/testdir/test_signals.mnv
blob: 11b24e9baa3438052a683079de982d4331638ca6 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
" Test signal handling.

CheckUnix

" Check whether a signal is available on this system.
func HasSignal(signal)
  let signals = system('kill -l')
  return signals =~# '\<' .. a:signal .. '\>'
endfunc

" Test signal WINCH (window resize signal)
func Test_signal_WINCH()
  CheckNotGui
  if !HasSignal('WINCH')
    throw 'Skipped: WINCH signal not supported'
  endif

  " We do not actually want to change the size of the terminal.
  let old_WS = ''
  if exists('&t_WS')
    let old_WS = &t_WS
    let &t_WS = ''
  endif

  let old_lines = &lines
  let old_columns = &columns
  let new_lines = &lines - 2
  let new_columns = &columns - 2

  exe 'set lines=' .. new_lines
  exe 'set columns=' .. new_columns
  call assert_equal(new_lines, &lines)
  call assert_equal(new_columns, &columns)

  " Send signal and wait for signal to be processed.
  " 'lines' and 'columns' should have been restored
  " after handing signal WINCH.
  exe 'silent !kill -s WINCH ' .. getpid()
  call WaitForAssert({-> assert_equal(old_lines, &lines)})
  call assert_equal(old_columns, &columns)

  if old_WS != ''
    let &t_WS = old_WS
  endif
endfunc

" Test signal PWR, which should update the swap file.
func Test_signal_PWR()
  if !HasSignal('PWR')
    throw 'Skipped: PWR signal not supported'
  endif

  " Set a very large 'updatetime' and 'updatecount', so that we can be sure
  " that swap file is updated as a result of sending PWR signal, and not
  " because of exceeding 'updatetime' or 'updatecount' when changing buffer.
  set updatetime=100000 updatecount=100000
  new Xtest_signal_PWR
  let swap_name = swapname('%')
  call setline(1, '123')
  preserve
  let swap_content = readfile(swap_name, 'b')

  " Update the buffer and check that the swap file is not yet updated,
  " since we set 'updatetime' and 'updatecount' to large values.
  call setline(1, 'abc')
  call assert_equal(swap_content, readfile(swap_name, 'b'))

  " Sending PWR signal should update the swap file.
  exe 'silent !kill -s PWR ' .. getpid()
  call WaitForAssert({-> assert_notequal(swap_content, readfile(swap_name, 'b'))})

  bwipe!
  set updatetime& updatecount&
endfunc

" Test signal INT. Handler sets got_int. It should be like typing CTRL-C.
func Test_signal_INT()
  CheckRunMNVInTerminal
  if !HasSignal('INT')
    throw 'Skipped: INT signal not supported'
  endif

  let buf = RunMNVInTerminal('', {'rows': 6})
  let pid_mnv = term_getjob(buf)->job_info().process

  " Check that an endless loop in MNV is interrupted by signal INT.
  call term_sendkeys(buf, ":call setline(1, 'running')\n")
  call term_sendkeys(buf, ":while 1 | endwhile\n")
  call WaitForAssert({-> assert_equal(':while 1 | endwhile', term_getline(buf, 6))})
  exe 'silent !kill -s INT ' .. pid_mnv
  sleep 50m
  call term_sendkeys(buf, ":call setline(1, 'INTERRUPTED')\n")
  call WaitForAssert({-> assert_equal('INTERRUPTED', term_getline(buf, 1))})

  call StopMNVInTerminal(buf)
endfunc

" Test signal TSTP. Handler sets got_tstp.
func Test_signal_TSTP()
  CheckRunMNVInTerminal
  if !HasSignal('TSTP')
    throw 'Skipped: TSTP signal not supported'
  endif

  " If test fails once, it can leave temporary files and trying to rerun
  " the test would then fail again if they are not deleted first.
  call delete('.Xsig_TERM.swp')
  call delete('XsetupAucmd')
  call delete('XautoOut1')
  call delete('XautoOut2')
  let lines =<< trim END
    au MNVSuspend * call writefile(["MNVSuspend triggered"], "XautoOut1", "as")
    au MNVResume * call writefile(["MNVResume triggered"], "XautoOut2", "as")
  END
  call writefile(lines, 'XsetupAucmd', 'D')

  let buf = RunMNVInTerminal('-S XsetupAucmd Xsig_TERM', {'rows': 6})
  let pid_mnv = term_getjob(buf)->job_info().process

  call term_sendkeys(buf, ":call setline(1, 'foo')\n")
  call WaitForAssert({-> assert_equal('foo', term_getline(buf, 1))})

  call assert_false(filereadable('Xsig_TERM'))

  " After TSTP the file is not saved (same function as ^Z)
  exe 'silent !kill -s TSTP ' .. pid_mnv
  call WaitForAssert({-> assert_true(filereadable('.Xsig_TERM.swp'))})
  sleep 100m

  " We resume after the suspend.  Sleep a bit for the signal to take effect,
  " also when running under valgrind.
  exe 'silent !kill -s CONT ' .. pid_mnv
  call WaitForAssert({-> assert_true(filereadable('XautoOut2'))})
  sleep 10m

  call StopMNVInTerminal(buf)

  let result = readfile('XautoOut1')
  call assert_equal(["MNVSuspend triggered"], result)
  let result = readfile('XautoOut2')
  call assert_equal(["MNVResume triggered"], result)

  %bwipe!
  call delete('.Xsig_TERM.swp')
  call delete('XautoOut1')
  call delete('XautoOut2')
endfunc

" Test a deadly signal.
"
" There are several deadly signals: SISEGV, SIBUS, SIGTERM...
" Test uses signal SIGTERM as it does not create a core
" dump file unlike SIGSEGV, SIGBUS, etc. See "man 7 signals.
"
" MNV should exit with a deadly signal and unsaved changes
" should be recoverable from the swap file preserved as a
" result of the deadly signal handler.
func Test_deadly_signal_TERM()
  if !HasSignal('TERM')
    throw 'Skipped: TERM signal not supported'
  endif
  CheckRunMNVInTerminal

  " If test fails once, it can leave temporary files and trying to rerun
  " the test would then fail again if they are not deleted first.
  call delete('.Xsig_TERM.swp')
  call delete('XsetupAucmd')
  call delete('XautoOut')
  let lines =<< trim END
    au MNVLeave * call writefile(["MNVLeave triggered"], "XautoOut", "as")
    au MNVLeavePre * call writefile(["MNVLeavePre triggered"], "XautoOut", "as")
  END
  call writefile(lines, 'XsetupAucmd', 'D')

  let buf = RunMNVInTerminal('-S XsetupAucmd Xsig_TERM', {'rows': 6})
  let pid_mnv = term_getjob(buf)->job_info().process

  call term_sendkeys(buf, ":call setline(1, 'foo')\n")
  call WaitForAssert({-> assert_equal('foo', term_getline(buf, 1))})

  call assert_false(filereadable('Xsig_TERM'))
  exe 'silent !kill -s TERM '  .. pid_mnv
  call WaitForAssert({-> assert_true(filereadable('.Xsig_TERM.swp'))})

  " Don't call StopMNVInTerminal() as it expects job to be still running.
  call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))})

  new
  silent recover .Xsig_TERM.swp
  call assert_equal(['foo'], getline(1, '$'))

  let result = readfile('XautoOut')
  call assert_equal(["MNVLeavePre triggered", "MNVLeave triggered"], result)

  %bwipe!
  call delete('.Xsig_TERM.swp')
  call delete('XautoOut')
endfunc

" mnv: ts=8 sw=2 sts=2 tw=80 fdm=marker