From 90f67e5204669264934f1ed0e2a6f047c75d1462 Mon Sep 17 00:00:00 2001
From: Sergey Yakubov <sergey.yakubov@desy.de>
Date: Mon, 9 Nov 2020 10:52:37 +0100
Subject: [PATCH] data in payload for python, tests

---
 consumer/api/python/asapo_consumer.pxd               |  2 +-
 producer/api/python/asapo_producer.pxd               |  5 +++--
 producer/api/python/asapo_producer.pyx.in            | 11 +++++++----
 tests/automatic/producer/python_api/check_linux.sh   | 12 +++++++++---
 .../automatic/producer/python_api/check_windows.bat  |  5 +++++
 tests/automatic/producer/python_api/producer_api.py  |  8 ++++----
 6 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/consumer/api/python/asapo_consumer.pxd b/consumer/api/python/asapo_consumer.pxd
index 692fb425b..867139750 100644
--- a/consumer/api/python/asapo_consumer.pxd
+++ b/consumer/api/python/asapo_consumer.pxd
@@ -24,7 +24,7 @@ cdef extern from "asapo_wrappers.h" namespace "asapo":
 
 cdef extern from "asapo_consumer.h" namespace "asapo":
   cppclass FileData:
-    unique_ptr[uint8_t[]] release()
+    uint8_t[] release()
     pass
 
 cdef extern from "asapo_consumer.h" namespace "asapo":
diff --git a/producer/api/python/asapo_producer.pxd b/producer/api/python/asapo_producer.pxd
index d0a53e6cb..26ec51600 100644
--- a/producer/api/python/asapo_producer.pxd
+++ b/producer/api/python/asapo_producer.pxd
@@ -26,7 +26,8 @@ cdef extern from "asapo_producer.h" namespace "asapo":
 
 cdef extern from "asapo_producer.h" namespace "asapo":
   cppclass FileData:
-    unique_ptr[uint8_t[]] release()
+    uint8_t[] release()
+    uint8_t[] get()
   cppclass StreamInfo:
     string Json(bool add_last_id)
     bool SetFromJson(string json_str, bool read_last_id)
@@ -81,9 +82,9 @@ cdef extern from "asapo_producer.h" namespace "asapo":
     string Json()
   struct RequestCallbackPayload:
     GenericRequestHeader original_header
+    FileData data
     string response
 
-
 cdef extern from "asapo_producer.h" namespace "asapo":
   cppclass RequestCallback:
     pass
diff --git a/producer/api/python/asapo_producer.pyx.in b/producer/api/python/asapo_producer.pyx.in
index 3cd30bf12..1319d9439 100644
--- a/producer/api/python/asapo_producer.pyx.in
+++ b/producer/api/python/asapo_producer.pyx.in
@@ -286,11 +286,14 @@ cdef class PyProducer:
         if err:
             throw_exception(err)
         return
-    cdef void c_callback_python(self,py_callback, RequestCallbackPayload payload, Error& err):
+    cdef void c_callback_python(self,py_callback, data, RequestCallbackPayload& payload, Error& err):
         if py_callback != None:
           info_str = _str(payload.original_header.Json())
           info = json.loads(info_str)
           info['server_response'] = payload.response
+          if payload.data.get() != NULL:
+             payload.data.release()
+             info['data'] = data
           if err:
             py_err = python_exception_from_error(err)
           else:
@@ -299,20 +302,20 @@ cdef class PyProducer:
           Py_XDECREF(<PyObject*>py_callback)
 
     cdef void c_callback(self,py_callback, RequestCallbackPayload payload, Error err) with gil:
-        self.c_callback_python(py_callback,payload,err)
+        self.c_callback_python(py_callback,None,payload,err)
 
     cdef void c_callback_ndarr(self,py_callback,nd_array,RequestCallbackPayload payload, Error err) with gil:
+        self.c_callback_python(py_callback,nd_array,payload,err)
         if nd_array is not None:
           if nd_array.base is not None:
             Py_XDECREF(<PyObject*>nd_array.base)
           else:
             Py_XDECREF(<PyObject*>nd_array)
-        self.c_callback_python(py_callback,payload,err)
 
     cdef void c_callback_bytesaddr(self,py_callback,bytes_array,RequestCallbackPayload payload, Error err) with gil:
+        self.c_callback_python(py_callback,bytes_array,payload,err)
         if bytes_array is not None:
             Py_XDECREF(<PyObject*>bytes_array)
-        self.c_callback_python(py_callback,payload,err)
     def cleanup(self):
         with  nogil:
             if self.c_producer.get() is not NULL:
diff --git a/tests/automatic/producer/python_api/check_linux.sh b/tests/automatic/producer/python_api/check_linux.sh
index b4fea561a..fe5ae2713 100644
--- a/tests/automatic/producer/python_api/check_linux.sh
+++ b/tests/automatic/producer/python_api/check_linux.sh
@@ -41,8 +41,14 @@ sleep 1
 
 $1 $3 $stream $beamtime_id  "127.0.0.1:8400" &> out || cat out
 cat out
-cat out | grep "successfuly sent" | wc -l | grep 13
+echo count successfully send, expect 13
+cat out | grep "successfuly sent" | wc -l | tee /dev/stderr | grep 13
+echo count same id, expect 4
+cat out | grep "already have record with same id" | wc -l | tee /dev/stderr | grep 4
+echo count duplicates, expect 4
+cat out | grep "duplicate" | wc -l | tee /dev/stderr | grep 4
+echo count data in callback, expect 3
+cat out | grep "'data':" | wc -l  | tee /dev/stderr | grep 3
+echo check found local io error
 cat out | grep "local i/o error"
-cat out | grep "already have record with same id" | wc -l | grep 4
-cat out | grep "duplicate" | wc -l | grep 4
 cat out | grep "Finished successfully"
diff --git a/tests/automatic/producer/python_api/check_windows.bat b/tests/automatic/producer/python_api/check_windows.bat
index a0a3537ef..6127144a3 100644
--- a/tests/automatic/producer/python_api/check_windows.bat
+++ b/tests/automatic/producer/python_api/check_windows.bat
@@ -33,6 +33,11 @@ echo %NUM% | findstr 1 || goto error
 for /F %%N in ('find /C "} server warning: duplicated request" ^< "out"') do set NUM=%%N
 echo %NUM% | findstr 1 || goto error
 
+for /F %%N in ("find /C 'data':") do set NUM=%%N
+echo %NUM% | findstr 3 || goto error
+
+
+
 findstr /I /L /C:"Finished successfully" out || goto :error
 
 goto :clean
diff --git a/tests/automatic/producer/python_api/producer_api.py b/tests/automatic/producer/python_api/producer_api.py
index 460a03ed1..dd247424c 100644
--- a/tests/automatic/producer/python_api/producer_api.py
+++ b/tests/automatic/producer/python_api/producer_api.py
@@ -26,14 +26,14 @@ def assert_eq(val, expected, name):
         sys.exit(1)
 
 
-def callback(header, err):
+def callback(payload, err):
     lock.acquire()  # to print
     if isinstance(err, asapo_producer.AsapoServerWarning):
-        print("successfuly sent, but with warning from server: ", header, err)
+        print("successfuly sent, but with warning from server: ", payload, err)
     elif err is not None:
-        print("could not sent: ", header, err)
+        print("could not sent: ", payload, err)
     else:
-        print("successfuly sent: ", header)
+        print("successfuly sent: ", payload)
     lock.release()
 
 
-- 
GitLab