/*
 * Copyright 2013-present Facebook, 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 <thread>

#include <folly/Memory.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/Request.h>
#include <folly/portability/GTest.h>

using namespace folly;

class TestData : public RequestData {
 public:
  explicit TestData(int data) : data_(data) {}
  ~TestData() override {}

  bool hasCallback() override {
    return true;
  }

  void onSet() override {
    set_++;
  }

  void onUnset() override {
    unset_++;
  }

  int set_ = 0, unset_ = 0;
  int data_;
};

RequestContext& getContext() {
  auto* ctx = RequestContext::get();
  EXPECT_TRUE(ctx != nullptr);
  return *ctx;
}

void setData(int data = 0) {
  getContext().setContextData("test", std::make_unique<TestData>(data));
}

bool hasData() {
  return getContext().getContextData("test") != nullptr;
}

const TestData& getData() {
  auto* ptr = dynamic_cast<TestData*>(getContext().getContextData("test"));
  EXPECT_TRUE(ptr != nullptr);
  return *ptr;
}

TEST(RequestContext, SimpleTest) {
  EventBase base;

  // There should always be a default context with get()
  EXPECT_TRUE(RequestContext::get() != nullptr);

  // but not with saveContext()
  EXPECT_EQ(RequestContext::saveContext(), nullptr);
  RequestContext::create();
  EXPECT_NE(RequestContext::saveContext(), nullptr);
  RequestContext::create();
  EXPECT_NE(RequestContext::saveContext(), nullptr);

  EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));

  RequestContext::get()->setContextData("test", std::make_unique<TestData>(10));
  base.runInEventBaseThread([&]() {
    EXPECT_TRUE(RequestContext::get() != nullptr);
    auto data =
        dynamic_cast<TestData*>(RequestContext::get()->getContextData("test"))
            ->data_;
    EXPECT_EQ(10, data);
    base.terminateLoopSoon();
  });
  auto th = std::thread([&]() { base.loopForever(); });
  th.join();
  EXPECT_TRUE(RequestContext::get() != nullptr);
  auto a =
      dynamic_cast<TestData*>(RequestContext::get()->getContextData("test"));
  auto data = a->data_;
  EXPECT_EQ(10, data);

  RequestContext::setContext(std::shared_ptr<RequestContext>());
  // There should always be a default context
  EXPECT_TRUE(nullptr != RequestContext::get());
}

TEST(RequestContext, RequestContextScopeGuard) {
  RequestContextScopeGuard g0;
  setData(10);
  {
    RequestContextScopeGuard g1;
    EXPECT_FALSE(hasData());
    setData(20);
    EXPECT_EQ(20, getData().data_);
    EXPECT_EQ(1, getData().set_);
    EXPECT_EQ(0, getData().unset_);
  }
  EXPECT_EQ(10, getData().data_);
  EXPECT_EQ(2, getData().set_);
  EXPECT_EQ(1, getData().unset_);
}

TEST(RequestContext, defaultContext) {
  // Don't create a top level guard
  // Regression test for set/onset used to not work with the default context
  setData(10);
  {
    RequestContextScopeGuard g1;
    EXPECT_FALSE(hasData());
  }
  EXPECT_EQ(10, getData().data_);
  EXPECT_EQ(2, getData().set_);
  EXPECT_EQ(1, getData().unset_);
}

TEST(RequestContext, setIfAbsentTest) {
  EXPECT_TRUE(RequestContext::get() != nullptr);

  RequestContext::get()->setContextData("test", std::make_unique<TestData>(10));
  EXPECT_FALSE(RequestContext::get()->setContextDataIfAbsent(
      "test", std::make_unique<TestData>(20)));
  EXPECT_EQ(
      10,
      dynamic_cast<TestData*>(RequestContext::get()->getContextData("test"))
          ->data_);

  EXPECT_TRUE(RequestContext::get()->setContextDataIfAbsent(
      "test2", std::make_unique<TestData>(20)));
  EXPECT_EQ(
      20,
      dynamic_cast<TestData*>(RequestContext::get()->getContextData("test2"))
          ->data_);

  RequestContext::setContext(std::shared_ptr<RequestContext>());
  EXPECT_TRUE(nullptr != RequestContext::get());
}

TEST(RequestContext, testSetUnset) {
  RequestContext::create();
  auto ctx1 = RequestContext::saveContext();
  ctx1->setContextData("test", std::make_unique<TestData>(10));
  auto testData1 = dynamic_cast<TestData*>(ctx1->getContextData("test"));

  // onSet called in setContextData
  EXPECT_EQ(1, testData1->set_);

  // Override RequestContext
  RequestContext::create();
  auto ctx2 = RequestContext::saveContext();
  ctx2->setContextData("test", std::make_unique<TestData>(20));
  auto testData2 = dynamic_cast<TestData*>(ctx2->getContextData("test"));

  // onSet called in setContextData
  EXPECT_EQ(1, testData2->set_);

  // Check ctx1->onUnset was called
  EXPECT_EQ(1, testData1->unset_);

  RequestContext::setContext(ctx1);
  EXPECT_EQ(2, testData1->set_);
  EXPECT_EQ(1, testData1->unset_);
  EXPECT_EQ(1, testData2->unset_);

  RequestContext::setContext(ctx2);
  EXPECT_EQ(2, testData1->set_);
  EXPECT_EQ(2, testData1->unset_);
  EXPECT_EQ(2, testData2->set_);
  EXPECT_EQ(1, testData2->unset_);
}

TEST(RequestContext, deadlockTest) {
  class DeadlockTestData : public RequestData {
   public:
    explicit DeadlockTestData(const std::string& val) : val_(val) {}

    ~DeadlockTestData() override {
      RequestContext::get()->setContextData(
          val_, std::make_unique<TestData>(1));
    }

    bool hasCallback() override {
      return false;
    }

    std::string val_;
  };

  RequestContext::get()->setContextData(
      "test", std::make_unique<DeadlockTestData>("test2"));
  RequestContext::get()->clearContextData("test");
}

TEST(RequestContext, Nested) {
  class NestableRequestData : public RequestData {
   public:
    explicit NestableRequestData() {}
    explicit NestableRequestData(int val) : val_(val) {}

    bool hasCallback() override {
      return false;
    }

    std::unique_ptr<RequestData> createChild() override {
      return std::make_unique<NestableRequestData>(val_ + 1);
    }

    int val_{0};

    static NestableRequestData* get() {
      return static_cast<NestableRequestData*>(
          RequestContext::get()->getContextData("nestable"));
    }
  };

  RootRequestContextGuard root;
  RequestContext::get()->setContextData(
      "nestable", make_unique<NestableRequestData>());
  RequestContext::get()->setContextData(
      "unnestable", make_unique<TestData>(42));
  EXPECT_EQ(NestableRequestData::get()->val_, 0);
  EXPECT_NE(RequestContext::get()->getContextData("unnestable"), nullptr);
  EXPECT_NE(NestableRequestData::get(), nullptr);
  {
    NestedRequestContextGuard nested1;
    EXPECT_EQ(NestableRequestData::get()->val_, 1);
    EXPECT_EQ(RequestContext::get()->getContextData("unnestable"), nullptr);
    NestedRequestContextGuard nested2;
    EXPECT_EQ(NestableRequestData::get()->val_, 2);
    RootRequestContextGuard newRoot;
    EXPECT_EQ(NestableRequestData::get(), nullptr);
  }
  {
    NestedRequestContextGuard nested1;
    EXPECT_EQ(NestableRequestData::get()->val_, 1);
  }
}
