patch 9.1.1090: tests: test_terminwscroll_topline2 unreliable

Problem:  tests: test_terminwscroll_topline2 unreliable
          (Yee Cheng Chin)
Solution: instead of using term_wait() with a specific time, use
          terminal-api and to wait until the terminal is finished

call a terminal callback function when finished printing, instead of
using term_wait(), with a defined time, which caused timeouts on CI
with the macos runners

Unfortunately I couldn't figure out how to call the terminal-api on Windows,
so skip the test on Windows. cmd.com echo didn's seem to work and
neither did trying to use python, but perhaps it was just me fighting
with the terminal quoting rules :shrug:

related: #16599
related: #16552

Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/testdir/test_terminal2.vim b/src/testdir/test_terminal2.vim
index c6edea9..5c4563c 100644
--- a/src/testdir/test_terminal2.vim
+++ b/src/testdir/test_terminal2.vim
@@ -290,11 +290,16 @@
 endfunc
 
 func Test_termwinscroll_topline2()
+  " calling the terminal API doesn't work on Windows
+  CheckNotMSWindows
   let g:test_is_flaky = 1
+  let g:print_complete = 0
+  func! Tapi_print_complete(bufnum, arglist)
+    let g:print_complete = 1
+  endfunc
+
   set termwinscroll=50000 mouse=a
-  if !has('win32')
-    set shell=sh
-  endif
+  set shell=sh
   let norm_winid = win_getid()
   terminal
   call assert_equal(2, winnr('$'))
@@ -304,26 +309,26 @@
 
   let num1 = &termwinscroll / 1000 * 999
   call writefile(range(num1), 'Xtext', 'D')
-  if has('win32')
-    call term_sendkeys(buf, "type Xtext\<CR>")
-  else
-    call term_sendkeys(buf, "cat Xtext\<CR>")
-  endif
+  call term_sendkeys(buf, "cat Xtext\<CR>")
+  call term_sendkeys(buf, 'printf ''\033]51;["call", "Tapi_print_complete", []]\007''' .. "\<cr>")
   let rows = term_getsize(buf)[0]
-  " It may take a while to finish on a slow system
-  call term_wait(buf, 2000 * g:run_nr)
-  " On MS-Windows there is an empty line, check both last line and above it.
+  let cnt = 0
+  while !g:print_complete && cnt <= 1000
+    " max number of runs
+    let cnt += 1
+    " sleep a bit, to give the the terminal some time to finish
+
+    " It may take a while to finish on a slow system
+    " so wait a bit and handle the callback
+    call term_wait(buf)
+  endwhile
   call WaitForAssert({-> assert_match(string(num1 - 1), term_getline(buf, rows - 1) .. '\|' .. term_getline(buf, rows - 2))})
   call feedkeys("\<C-W>N", 'xt')
   call feedkeys("i", 'xt')
 
   let num2 = &termwinscroll / 1000 * 8
   call writefile(range(num2), 'Xtext', 'D')
-  if has('win32')
-    call term_sendkeys(buf, "timeout /t 2 && type Xtext\<CR>")
-  else
-    call term_sendkeys(buf, "sleep 2; cat Xtext\<CR>")
-  endif
+  call term_sendkeys(buf, "sleep 2; cat Xtext\<CR>")
   let winrow = get(get(filter(getwininfo(), 'v:val.winid == norm_winid'), 0, {}), 'winrow', -1)
 
   call test_setmouse(winrow, 1)
@@ -342,6 +347,8 @@
 
   exe buf . 'bwipe!'
   set termwinscroll& mouse& sh&
+  delfunc Tapi_print_complete
+  unlet! g:print_complete
 endfunc
 
 " Resizing the terminal window caused an ml_get error.
diff --git a/src/version.c b/src/version.c
index 28234de..580f82a 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1090,
+/**/
     1089,
 /**/
     1088,