<commit_before>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2016 Couchbase, Inc
 *
 *   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 "dcp-producer.h"
#include "dcp-stream.h"
#include "../programs/engine_testapp/mock_server.h"

#include <platform/dirutils.h>

// Simple backport of the GTest-style EXPECT_EQ macro.
#define EXPECT_EQ(a, b, msg) \
    do { \
        if ((a) != (b)) { \
            throw std::runtime_error(msg); \
        } \
    } while(0)

// Mock of the ActiveStream class. Wraps the real ActiveStream, but exposes
// normally protected methods publically for test purposes.
class MockActiveStream : public ActiveStream {
public:
    MockActiveStream(EventuallyPersistentEngine* e, dcp_producer_t p,
                     const std::string &name, uint32_t flags, uint32_t opaque,
                     uint16_t vb, uint64_t st_seqno, uint64_t en_seqno,
                     uint64_t vb_uuid, uint64_t snap_start_seqno,
                     uint64_t snap_end_seqno)
    : ActiveStream(e, p, name, flags, opaque, vb, st_seqno, en_seqno, vb_uuid,
                   snap_start_seqno, snap_end_seqno) {}

    // Expose underlying protected ActiveStream methods as public
    void public_getOutstandingItems(RCPtr<VBucket> &vb,
                                    std::deque<queued_item> &items) {
        getOutstandingItems(vb, items);
    }

    void public_processItems(std::deque<queued_item>& items) {
        processItems(items);
    }

    bool public_nextCheckpointItem() {
        return nextCheckpointItem();
    }
};

/* Regression test for MB-17766 - ensure that when an ActiveStream is preparing
 * queued items to be sent out via a DCP consumer, that nextCheckpointItem()
 * doesn't incorrectly return false (meaning that there are no more checkpoint
 * items to send).
 */
static void test_mb17766(const std::string& test_dbname) {
    // Setup an engine with a single active vBucket.
    ENGINE_HANDLE* handle;
    EXPECT_EQ(ENGINE_SUCCESS,
              create_instance(1, get_mock_server_api, &handle),
              "Failed to created ep engine instance");

    EventuallyPersistentEngine* engine =
            reinterpret_cast<EventuallyPersistentEngine*>(handle);
    ObjectRegistry::onSwitchThread(engine);
    std::string config = "dbname=" + test_dbname;
    EXPECT_EQ(ENGINE_SUCCESS,
              engine->initialize(config.c_str()),
              "Failed to initialize engine.");

    const uint16_t vbid = 0;
    engine->setVBucketState(vbid, vbucket_state_active, false);

    // Wait for warmup to complete.
    while (engine->getEpStore()->isWarmingUp()) {
        usleep(10);
    }

    // Set AuxIO threads to zero, so that the producer's
    // ActiveStreamCheckpointProcesserTask doesn't run.
    ExecutorPool::get()->setMaxAuxIO(0);

    // Add an item.<commit_msg>MB-19360: Init mock server in stream module tests<commit_after>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2016 Couchbase, Inc
 *
 *   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 "dcp-producer.h"
#include "dcp-stream.h"
#include "../programs/engine_testapp/mock_server.h"

#include <platform/dirutils.h>

// Simple backport of the GTest-style EXPECT_EQ macro.
#define EXPECT_EQ(a, b, msg) \
    do { \
        if ((a) != (b)) { \
            throw std::runtime_error(msg); \
        } \
    } while(0)

// Mock of the ActiveStream class. Wraps the real ActiveStream, but exposes
// normally protected methods publically for test purposes.
class MockActiveStream : public ActiveStream {
public:
    MockActiveStream(EventuallyPersistentEngine* e, dcp_producer_t p,
                     const std::string &name, uint32_t flags, uint32_t opaque,
                     uint16_t vb, uint64_t st_seqno, uint64_t en_seqno,
                     uint64_t vb_uuid, uint64_t snap_start_seqno,
                     uint64_t snap_end_seqno)
    : ActiveStream(e, p, name, flags, opaque, vb, st_seqno, en_seqno, vb_uuid,
                   snap_start_seqno, snap_end_seqno) {}

    // Expose underlying protected ActiveStream methods as public
    void public_getOutstandingItems(RCPtr<VBucket> &vb,
                                    std::deque<queued_item> &items) {
        getOutstandingItems(vb, items);
    }

    void public_processItems(std::deque<queued_item>& items) {
        processItems(items);
    }

    bool public_nextCheckpointItem() {
        return nextCheckpointItem();
    }
};

/* Regression test for MB-17766 - ensure that when an ActiveStream is preparing
 * queued items to be sent out via a DCP consumer, that nextCheckpointItem()
 * doesn't incorrectly return false (meaning that there are no more checkpoint
 * items to send).
 */
static void test_mb17766(const std::string& test_dbname) {
    // Setup an engine with a single active vBucket.
    ENGINE_HANDLE* handle;
    EXPECT_EQ(ENGINE_SUCCESS,
              create_instance(1, get_mock_server_api, &handle),
              "Failed to created ep engine instance");

    // Init mock server to initialize time_mutex used in
    // mock_get_current_time & mock_time_travel apis.
    init_mock_server(handle);

    EventuallyPersistentEngine* engine =
            reinterpret_cast<EventuallyPersistentEngine*>(handle);
    ObjectRegistry::onSwitchThread(engine);
    std::string config = "dbname=" + test_dbname;
    EXPECT_EQ(ENGINE_SUCCESS,
              engine->initialize(config.c_str()),
              "Failed to initialize engine.");

    const uint16_t vbid = 0;
    engine->setVBucketState(vbid, vbucket_state_active, false);

    // Wait for warmup to complete.
    while (engine->getEpStore()->isWarmingUp()) {
        usleep(10);
    }

    // Set AuxIO threads to zero, so that the producer's
    // ActiveStreamCheckpointProcesserTask doesn't run.
    ExecutorPool::get()->setMaxAuxIO(0);

    // Add an item.
{'fraction_non_alphanumeric': 0.09576295448038935, 'fraction_numerical': 0.01331233896364157, 'mean_word_length': 2.8989955357142856, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1666529', 'n_tokens_mistral': 2132, 'n_tokens_neox': 2026, 'n_words': 624}