summaryrefslogtreecommitdiff
path: root/test/storage/http_issue_1369.cpp
blob: 3d7165d9bed8ecfb45688c2866f7407d9a462b1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "storage.hpp"

#include <uv.h>

#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/sqlite_cache.hpp>
#include <mbgl/util/run_loop.hpp>

// Test for https://github.com/mapbox/mapbox-gl-native/issue/1369
//
// A request for http://example.com is made. This triggers a cache get. While the cache get is
// pending, the request is canceled. This removes it from pending. Then, still while the cache get
// is pending, a second request is made for the same resource. This adds an entry back to pending
// and queues another cache request, even though the first one is still pending. Now both cache
// requests resolve to misses, resulting in two HTTP requests for the same resource. The first one
// will notify as expected, the second one will have bound a DefaultFileRequest* in the lambda that
// gets invalidated by the first notify's pending.erase, and when it gets notified, the crash
// occurs.

TEST_F(Storage, HTTPIssue1369) {
    SCOPED_TEST(HTTPIssue1369)

    using namespace mbgl;

    SQLiteCache cache;
    DefaultFileSource fs(&cache);
    util::RunLoop loop(uv_default_loop());

    const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/test" };

    auto req = fs.request(resource, [&](const Response&) {
        ADD_FAILURE() << "Callback should not be called";
    });
    fs.cancel(req);
    req = fs.request(resource, [&](const Response &res) {
        fs.cancel(req);
        EXPECT_EQ(nullptr, res.error);
        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);
        loop.stop();
        HTTPIssue1369.finish();
    });

    uv_run(uv_default_loop(), UV_RUN_DEFAULT);
}