<commit_before>// Copyright (c) 2020 by Apex.AI Inc. All rights reserved.
//
// Licensed 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 "iceoryx_posh/internal/popo/building_blocks/condition_variable_data.hpp"
#include "iceoryx_posh/internal/popo/building_blocks/condition_variable_waiter.hpp"
#include "iceoryx_posh/popo/trigger_handle.hpp"

#include "test.hpp"
#include <thread>

using namespace iox;
using namespace iox::popo;
using namespace ::testing;

class TriggerHandle_test : public Test
{
  public:
    void resetCallback(const uint64_t id)
    {
        m_resetCallbackId = id;
    }
    uint64_t m_resetCallbackId = 0U;
    ConditionVariableData m_condVar;
    TriggerHandle_test* m_self = this;

    TriggerHandle m_sut{&m_condVar, {this, &TriggerHandle_test::resetCallback}, 12};
};


TEST_F(TriggerHandle_test, isValidWhenConditionVariableIsNotNull)
{
    EXPECT_TRUE(m_sut.isValid());
    EXPECT_TRUE(m_sut);
}

TEST_F(TriggerHandle_test, isNotValidWhenConditionVariableIsNull)
{
    TriggerHandle sut2{nullptr, {m_self, &TriggerHandle_test::resetCallback}, 12};

    EXPECT_FALSE(sut2.isValid());
    EXPECT_FALSE(sut2);
}

TEST_F(TriggerHandle_test, InvalidateCreatesInvalidTriggerHandle)
{
    m_sut.invalidate();

    EXPECT_FALSE(m_sut.isValid());
    EXPECT_FALSE(m_sut);
}

TEST_F(TriggerHandle_test, ResetCreatesInvalidTriggerHandle)
{
    m_sut.reset();

    EXPECT_FALSE(m_sut.isValid());
    EXPECT_FALSE(m_sut);
}

TEST_F(TriggerHandle_test, ResetCallsResetCallbackWhenHandleIsValid)
{
    m_sut.reset();
    EXPECT_EQ(m_resetCallbackId, 12);
}

TEST_F(TriggerHandle_test, ResetDoesNotCallResetCallbackWhenHandleIsInvalid)
{
    m_sut.invalidate();
    m_sut.reset();
    EXPECT_EQ(m_resetCallbackId, 0);
}

TEST_F(TriggerHandle_test, getConditionVariableDataReturnsCorrectVar)
{
    EXPECT_EQ(m_sut.getConditionVariableData(), &m_condVar);
}

TEST_F(TriggerHandle_test, getUniqueIdReturnsCorrectId)
{
    TriggerHandle sut2{nullptr, {m_self, &TriggerHandle_test::resetCallback}, 8912};
    EXPECT_EQ(sut2.getUniqueId(), 8912);
}

TEST_F(TriggerHandle_test, triggerNotifiesConditionVariable)
{
    std::atomic_int stage{0};

    std::thread t([&] {
        while (stage.load() != 1)
            ;

        m_sut.trigger();
        stage.store(2);
        m_sut.trigger();
    });

    stage.store(1);
    ConditionVariableWaiter(&m_condVar).timedWait(100_ms);
    ConditionVariableWaiter(&m_condVar).timedWait(100_ms);

    EXPECT_EQ(stage.load(), 2);
    t.join();
}<commit_msg>iox-#341 refined trigger test<commit_after>// Copyright (c) 2020 by Apex.AI Inc. All rights reserved.
//
// Licensed 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 "iceoryx_posh/internal/popo/building_blocks/condition_variable_data.hpp"
#include "iceoryx_posh/internal/popo/building_blocks/condition_variable_waiter.hpp"
#include "iceoryx_posh/popo/trigger_handle.hpp"

#include "test.hpp"
#include <thread>

using namespace iox;
using namespace iox::popo;
using namespace ::testing;

class TriggerHandle_test : public Test
{
  public:
    void resetCallback(const uint64_t id)
    {
        m_resetCallbackId = id;
    }
    uint64_t m_resetCallbackId = 0U;
    ConditionVariableData m_condVar;
    TriggerHandle_test* m_self = this;

    TriggerHandle m_sut{&m_condVar, {this, &TriggerHandle_test::resetCallback}, 12};
};


TEST_F(TriggerHandle_test, isValidWhenConditionVariableIsNotNull)
{
    EXPECT_TRUE(m_sut.isValid());
    EXPECT_TRUE(m_sut);
}

TEST_F(TriggerHandle_test, isNotValidWhenConditionVariableIsNull)
{
    TriggerHandle sut2{nullptr, {m_self, &TriggerHandle_test::resetCallback}, 12};

    EXPECT_FALSE(sut2.isValid());
    EXPECT_FALSE(sut2);
}

TEST_F(TriggerHandle_test, InvalidateCreatesInvalidTriggerHandle)
{
    m_sut.invalidate();

    EXPECT_FALSE(m_sut.isValid());
    EXPECT_FALSE(m_sut);
}

TEST_F(TriggerHandle_test, ResetCreatesInvalidTriggerHandle)
{
    m_sut.reset();

    EXPECT_FALSE(m_sut.isValid());
    EXPECT_FALSE(m_sut);
}

TEST_F(TriggerHandle_test, ResetCallsResetCallbackWhenHandleIsValid)
{
    m_sut.reset();
    EXPECT_EQ(m_resetCallbackId, 12);
}

TEST_F(TriggerHandle_test, ResetDoesNotCallResetCallbackWhenHandleIsInvalid)
{
    m_sut.invalidate();
    m_sut.reset();
    EXPECT_EQ(m_resetCallbackId, 0);
}

TEST_F(TriggerHandle_test, getConditionVariableDataReturnsCorrectVar)
{
    EXPECT_EQ(m_sut.getConditionVariableData(), &m_condVar);
}

TEST_F(TriggerHandle_test, getUniqueIdReturnsCorrectId)
{
    TriggerHandle sut2{nullptr, {m_self, &TriggerHandle_test::resetCallback}, 8912};
    EXPECT_EQ(sut2.getUniqueId(), 8912);
}

TEST_F(TriggerHandle_test, triggerNotifiesConditionVariable)
{
    std::atomic_int stage{0};

    std::thread t([&] {
        ConditionVariableWaiter(&m_condVar).wait();
        stage.store(1);
    });

    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    EXPECT_EQ(stage.load(), 0);
    m_sut.trigger();
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    EXPECT_EQ(stage.load(), 1);

    t.join();
}

{'fraction_non_alphanumeric': 0.1122702434177844, 'fraction_numerical': 0.014903129657228018, 'mean_word_length': 4.864077669902913, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2650099', 'n_tokens_mistral': 2066, 'n_tokens_neox': 2012, 'n_words': 393}