stegemr commented on a change in pull request #363:
URL: https://github.com/apache/celix/pull/363#discussion_r718392259



##########
File path: libs/pushstreams/gtest/src/PushStreamTestSuite.cc
##########
@@ -0,0 +1,695 @@
+/**
+ *Licensed to the Apache Software Foundation (ASF) under one
+ *or more contributor license agreements.  See the NOTICE file
+ *distributed with this work for additional information
+ *regarding copyright ownership.  The ASF licenses this file
+ *to you under the Apache License, Version 2.0 (the
+ *"License"); you may not use this file except in compliance
+ *with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *Unless required by applicable law or agreed to in writing,
+ *software distributed under the License is distributed on an
+ *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ *specific language governing permissions and limitations
+ *under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include "celix/PushStreamProvider.h"
+
+using celix::PushStreamProvider;
+
+class TestException : public std::exception {
+public:
+    explicit TestException(const char* what) : w{what} {}
+    explicit TestException(std::string what) : w{std::move(what)} {}
+
+    TestException(const TestException&) = delete;
+    TestException(TestException&&) noexcept = default;
+
+    TestException& operator=(const TestException&) = delete;
+    TestException& operator=(TestException&&) noexcept = default;
+
+    [[nodiscard]] const char* what() const noexcept override { return 
w.c_str(); }
+private:
+    std::string w;
+};
+
+class EventObject {
+public:
+    EventObject() : val{0} {
+    }
+
+    explicit EventObject(int _val) : val{_val} {
+    }
+
+    EventObject(const EventObject& _val) = default;
+    EventObject& operator=(const EventObject& other) = default;
+
+    EventObject& operator=(int other) {
+        val = other;
+        return *this;
+    };
+
+    friend EventObject operator+(const EventObject &eo1, const EventObject  
&eo2) {
+        return EventObject{eo1.val + eo2.val};
+    }
+    friend int operator+(const int &eo1, const EventObject  &eo2) {
+        return eo1 + eo2.val;
+    }
+    friend int operator+(const EventObject &eo1, const int  &eo2) {
+        return eo1.val + eo2;
+    }
+    int val;
+};
+
+class PushStreamTestSuite : public ::testing::Test {
+public:
+    ~PushStreamTestSuite() noexcept override = default;
+
+    PushStreamProvider psp {};
+    std::unique_ptr<std::thread> t{};
+
+    std::shared_ptr<celix::IExecutor> executor 
{std::make_shared<celix::DefaultExecutor>()};
+    celix::PromiseFactory promiseFactory {executor};
+    celix::Deferred<void> done{promiseFactory.deferred<void>()};
+    celix::Promise<void> donepromise = done.getPromise();
+
+    template <typename T>
+    std::shared_ptr<celix::SynchronousPushEventSource<T>> createEventSource(T 
event, int publishCount, bool autoinc = false) {
+        auto ses = psp.template createSynchronousEventSource<T>();
+
+        auto successLambda = [this, ses, event, publishCount, 
autoinc](celix::Promise<void> p) -> celix::Promise<void> {
+            t = std::make_unique<std::thread>([&, event, publishCount, 
autoinc]() {
+                int counter = 0;
+                T data {event};
+                // Keep going as long as someone is listening
+                while (counter < publishCount) {
+                    ses->publish(data);
+                    if (autoinc) {
+                        data = data + 1;
+                    }
+                    counter++;
+                }
+            });
+
+            t->join();
+            ses->close();
+            done.resolve();
+            return p;
+        };
+
+        auto x = ses->connectPromise().template then<void>(successLambda);
+
+        return ses;
+    }
+};
+
+TEST_F(PushStreamTestSuite, EventSourceCloseTest) {
+    int onClosedReceived{0};
+    int onErrorReceived{0};
+
+    auto ses = psp.template createSynchronousEventSource<int>();
+
+    auto successLambda = [](celix::Promise<void> p) -> celix::Promise<void> {
+        return p;
+    };
+    auto x = ses->connectPromise().then<void>(successLambda);
+    auto stream = psp.createUnbufferedStream<int>(ses);
+
+    auto streamEnded = stream->onClose([&](){
+        onClosedReceived++;
+    }).onError([&](){
+        onErrorReceived++;
+    }).forEach([&](int /*event*/) { });
+
+    ses->close();
+
+    streamEnded.wait();
+
+    ASSERT_EQ(1, onClosedReceived);
+    ASSERT_EQ(0, onErrorReceived);
+}
+
+TEST_F(PushStreamTestSuite, ChainedEventSourceCloseTest) {
+    int onClosedReceived{0};
+    int onErrorReceived{0};
+
+    auto ses = psp.template createSynchronousEventSource<int>();
+
+    auto successLambda = [](celix::Promise<void> p) -> celix::Promise<void> {
+        return p;
+    };
+    auto x = ses->connectPromise().then<void>(successLambda);
+
+    auto stream = psp.createUnbufferedStream<int>(ses);
+    auto& filteredStream = stream->filter([](const int& /*event*/) -> bool {
+            return true;
+        }).onClose([&](){
+            onClosedReceived++;
+        }).onError([&](){
+            onErrorReceived++;
+        });
+
+    auto streamEnded = filteredStream.forEach([&](int /*event*/) { });
+
+    ses->close();
+
+    streamEnded.wait();
+
+    ASSERT_EQ(1, onClosedReceived);
+    ASSERT_EQ(0, onErrorReceived);
+}
+
+
+TEST_F(PushStreamTestSuite, StreamCloseTest) {
+    int onClosedReceived{0};
+    int onErrorReceived{0};
+
+    auto ses = psp.createSynchronousEventSource<int>();
+
+    auto successLambda = [](celix::Promise<void> p) -> celix::Promise<void> {
+        return p;
+    };
+    auto x = ses->connectPromise().then<void>(successLambda);
+
+    auto stream = psp.createUnbufferedStream<int>(ses);
+    auto streamEnded = stream->onClose([&](){
+        onClosedReceived++;
+    }).onError([&](){
+        onErrorReceived++;
+    }).forEach([&](int /*event*/) { });
+
+    stream->close();
+
+    streamEnded.wait();
+
+    ASSERT_EQ(1, onClosedReceived);
+    ASSERT_EQ(0, onErrorReceived);
+}
+
+TEST_F(PushStreamTestSuite, PublishAfterStreamCloseTest) {
+    int onClosedReceived{0};
+    int onErrorReceived{0};
+    int onEventReceived{0};
+
+    auto ses = psp.createSynchronousEventSource<int>();
+
+    auto successLambda = [](celix::Promise<void> p) -> celix::Promise<void> {
+        return p;
+    };
+    auto x = ses->connectPromise().then<void>(successLambda);

Review comment:
       They do nothing, dead code removed

##########
File path: libs/pushstreams/docs/.gitignore
##########
@@ -0,0 +1 @@
+*.png

Review comment:
       Done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@celix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to