chromium/net/base/test_completion_callback.h

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_BASE_TEST_COMPLETION_CALLBACK_H_
#define NET_BASE_TEST_COMPLETION_CALLBACK_H_

#include <stdint.h>

#include <memory>
#include <optional>
#include <utility>

#include "base/compiler_specific.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "net/base/completion_once_callback.h"
#include "net/base/net_errors.h"

//-----------------------------------------------------------------------------
// completion callback helper

// A helper class for completion callbacks, designed to make it easy to run
// tests involving asynchronous operations.  Just call WaitForResult to wait
// for the asynchronous operation to complete.  Uses a RunLoop to spin the
// current MessageLoop while waiting.  The callback must be invoked on the same
// thread WaitForResult is called on.
//
// NOTE: Since this runs a message loop to wait for the completion callback,
// there could be other side-effects resulting from WaitForResult.  For this
// reason, this class is probably not ideal for a general application.
//
namespace base {
class RunLoop;
}

namespace net {

class IOBuffer;

namespace internal {

class TestCompletionCallbackBaseInternal {};

template <typename R>
struct NetErrorIsPendingHelper {};

template <typename R, typename IsPendingHelper = NetErrorIsPendingHelper<R>>
class TestCompletionCallbackTemplate
    : public TestCompletionCallbackBaseInternal {
 public:
  TestCompletionCallbackTemplate(const TestCompletionCallbackTemplate&) =
      delete;
  TestCompletionCallbackTemplate& operator=(
      const TestCompletionCallbackTemplate&) = delete;
  ~TestCompletionCallbackTemplate() override = default;

  R WaitForResult() {}

  R GetResult(R result) {}

 protected:
  TestCompletionCallbackTemplate() :{}

  // Override this method to gain control as the callback is running.
  virtual void SetResult(R result) {}

 private:
  R result_;
};

}  // namespace internal

class TestClosure : public internal::TestCompletionCallbackBaseInternal {};

// Base class overridden by custom implementations of TestCompletionCallback.
TestCompletionCallbackBase;

TestInt64CompletionCallbackBase;

class TestCompletionCallback : public TestCompletionCallbackBase {};

class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase {};

// Makes sure that the buffer is not referenced when the callback runs.
class ReleaseBufferCompletionCallback: public TestCompletionCallback {};

}  // namespace net

#endif  // NET_BASE_TEST_COMPLETION_CALLBACK_H_