we are now scoping all file requests to an environment object. The FileSource implementation treats this as an opaque pointer, but allows canceling all Requests that are associated with that pointer. This is necessary to abort all file requests that originated from a particular Map object. Aborting a file request is different from canceling a file request: A canceled request doesn't have its callback called, while an aborted request will have its callback called with an error, indicating that the environment is going to be shut down.
55 lines
1.8 KiB
C++
55 lines
1.8 KiB
C++
#include "storage.hpp"
|
|
|
|
#include <uv.h>
|
|
|
|
#include <mbgl/storage/default_file_source.hpp>
|
|
#include <mbgl/storage/network_status.hpp>
|
|
|
|
#include <cmath>
|
|
|
|
TEST_F(Storage, HTTPCancelEnvironment) {
|
|
SCOPED_TEST(HTTPRetainedEnvironment)
|
|
SCOPED_TEST(HTTPCanceledEnvironment)
|
|
|
|
using namespace mbgl;
|
|
|
|
DefaultFileSource fs(nullptr, uv_default_loop());
|
|
|
|
// Create two fake environment pointers. The FileSource implementation treats these as opaque
|
|
// pointers and doesn't reach into them.
|
|
auto &env1 = *reinterpret_cast<const Environment *>(1);
|
|
auto &env2 = *reinterpret_cast<const Environment *>(2);
|
|
|
|
const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/delayed" };
|
|
|
|
// Environment 1
|
|
fs.request(resource, uv_default_loop(), env1, [&](const Response &res) {
|
|
// This environment gets aborted below. This means the request is marked as failing and
|
|
// will return an error here.
|
|
EXPECT_EQ(Response::Error, res.status);
|
|
EXPECT_EQ("", res.data);
|
|
EXPECT_EQ(0, res.expires);
|
|
EXPECT_EQ(0, res.modified);
|
|
EXPECT_EQ("", res.etag);
|
|
EXPECT_EQ("Environment is terminating", res.message);
|
|
HTTPCanceledEnvironment.finish();
|
|
});
|
|
|
|
// Environment 2
|
|
fs.request(resource, uv_default_loop(), env2, [&](const Response &res) {
|
|
// The same request as above, but in a different environment which doesn't get aborted. This
|
|
// means the request should succeed.
|
|
EXPECT_EQ(Response::Successful, res.status);
|
|
EXPECT_EQ("Response", res.data);
|
|
EXPECT_EQ(0, res.expires);
|
|
EXPECT_EQ(0, res.modified);
|
|
EXPECT_EQ("", res.etag);
|
|
EXPECT_EQ("", res.message);
|
|
HTTPRetainedEnvironment.finish();
|
|
});
|
|
|
|
fs.abort(env1);
|
|
|
|
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
|
}
|