Example #1
0
    using namespace mbgl;

    DefaultFileSource fs(nullptr);

    const int concurrency = 50;
    const int max = 10000;
    int number = 1;

    Request* reqs[concurrency];

    std::function<void(int)> req = [&](int i) {
        const auto current = number++;
        reqs[i] = fs.request({ Resource::Unknown,
                     std::string("http://127.0.0.1:3000/load/") + std::to_string(current) },
                   uv_default_loop(), [&, i, current](const Response &res) {
            fs.cancel(reqs[i]);
            reqs[i] = nullptr;
            EXPECT_EQ(nullptr, res.error);
            EXPECT_EQ(false, res.stale);
            ASSERT_TRUE(res.data.get());
            EXPECT_EQ(std::string("Request ") +  std::to_string(current), *res.data);
            EXPECT_EQ(0, res.expires);
            EXPECT_EQ(0, res.modified);
            EXPECT_EQ("", res.etag);

            if (number <= max) {
                req(i);
            } else if (current == max) {
                HTTPLoad.finish();
            }
        });
        EXPECT_EQ(false, res.stale);
        ASSERT_TRUE(res.data.get());
        EXPECT_EQ("Response", *res.data);
        EXPECT_EQ(0, res.expires);
        EXPECT_EQ(0, res.modified);
        EXPECT_EQ("snowfall", res.etag);
        EXPECT_EQ("", res.message);

        req2 = fs.request(revalidateSame, uv_default_loop(), [&, res](const Response &res2) {
            if (res2.stale) {
                // Discard stale responses, if any.
                return;
            }

            ASSERT_TRUE(req1);
            fs.cancel(req1);
            req1 = nullptr;

            ASSERT_TRUE(req2);
            fs.cancel(req2);
            req2 = nullptr;

            EXPECT_EQ(Response::Successful, res2.status);
            EXPECT_EQ(false, res.stale);
            ASSERT_TRUE(res2.data.get());
            EXPECT_EQ(res.data, res2.data);
            EXPECT_EQ("Response", *res2.data);
            // We use this to indicate that a 304 reply came back.
            EXPECT_LT(0, res2.expires);
            EXPECT_EQ(0, res2.modified);
            // We're not sending the ETag in the 304 reply, but it should still be there.
#include "storage.hpp"

#include <uv.h>

#include <mbgl/storage/default_file_source.hpp>

TEST_F(Storage, HTTPOtherLoop) {
    SCOPED_TEST(HTTPOtherLoop)

    using namespace mbgl;

    // This file source launches a separate thread to do the processing.
    DefaultFileSource fs(nullptr);

    Request* req = fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test" }, uv_default_loop(),
               [&](const Response &res) {
        fs.cancel(req);
        EXPECT_EQ(Response::Successful, res.status);
        EXPECT_EQ(false, res.stale);
        ASSERT_TRUE(res.data.get());
        EXPECT_EQ("Hello World!", *res.data);
        EXPECT_EQ(0, res.expires);
        EXPECT_EQ(0, res.modified);
        EXPECT_EQ("", res.etag);
        EXPECT_EQ("", res.message);
        HTTPOtherLoop.finish();
    });

    uv_run(uv_default_loop(), UV_RUN_DEFAULT);
}