!******************************************************************************!
! Non-interactive test cases for SOURCEDG.
! See test_sinkdg.mm for normal operational test cases.
!
! Arguments: [optional] single test case to execute and exit
!
! @bug      2368 SOURCEDG: thread unsafe usage of XMLTab variable
! @author:  Neon Ngo
!******************************************************************************!
startmacro S:func[""]
  switch BASELINE    baseline    GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)
  switch DEBUG       debug       GET DEF=FALSE SDEF=TRUE
  switch INTERACTIVE interactive GET DEF=TRUE  SDEF=TRUE

  if func NEQS "" then
    call test_^{func}
    stop
  endif

  ! NTN: Until [BUG 1965 NeXtOpt reads NM_USER_JVM_FLAGS]
  ! AND/OR possibly [BUG 1864 dgnetsend and Datagram.java multicast construction error or RH 5.2]
  ! we will have to skip the below test case while running inside X-Midas
  invoke isGradleBuild nxm.sys.lib.Shell.isGradleBuild()
  if isGradleBuild isFalse then
    if reg.shell.isNeXtOpt isFalse then
      call test_bug2368
    endif
  else
    info "SKIPPING ^reg.main.name as it does not pass when running from a gradle build"
  endif
endmacro

!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "XMLMSGS" then
    calc L:recvpackets recvpackets 1 +
    if debug then say "Got RTP with XML data: ^msg.data"
    ! test case for BUG 2368
    assert/text="^reg.main.name: Got valid RTP XML data packet (^msg.data.x)" msg.data neqs "" AND msg.data.x rexists
    ! got all sent packets, send TIMER message to self to verify results and end test case now
    if recvpackets >= sentpackets then sendto MAIN TIMER
  elseif msg.name eqs "TIMER" then
    ! got timer to end test case for BUG 2368
    assert/text="^reg.main.name: sent ^sentpackets data packets (expected ^expSentpackets)" expSentpackets == sentpackets
    assert/text="^reg.main.name: received ^recvpackets data packets (expected 1+)" recvpackets >= 1
    pipe stop
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! test BUG 2368 SOURCEDG: thread unsafe usage of XMLTab variable
! by sending a burst of RTP packets with XML data to verify that the receiving
! end (i.e. in the processMessage procedure via SOURCEDG) does not get back an
! empty table of the converted XML data in msg.data
procedure test_bug2368 L:count[10]
  set expSentpackets count
  set L:sentpackets 0
  set L:recvpackets 0
  ! use a random multicast group IP and port to minimize collision from other instances of this test case running
  calc L:randomIP   random 199 * 1 +      ! limit last part of multicast group IP address to [1-199]
  calc L:randomPort random 10 * 18800 +   ! limit port to [18800-18809]
! calc L:randomPort random 10000 * 1000 + ! orig prior to 3.3.0
  set hostport "239.1.1.^{randomIP}:^{randomPort}"
  pipe on
    sinkdg/rtp/hostport=hostport
    sourcedg/msgonly/msgname="XMLMSGS"/on/msgid=main hostport=hostport

    ! burst send count RTP packets with XML data
    timex now sod=sod
    do ii 1 count
      set data "<TESTXML X="^{ii}" FROM="^{env.hostaddr}" />"
      if debug then say "sending RTP packet data=^data"
      invoke sent reg.sinkdg.sendRTP(data)
      if sent > 0 then calc L:sentpackets sentpackets 1 +
    enddo

    ! set timer to end test case (give quarter second for each packet sent)
    set this.timer(0) count*0.25
  pipe off
return
