summaryrefslogtreecommitdiffstats
path: root/blimp/net/blimp_connection_unittest.cc
blob: 759b426d7ed7d6c6b506bc776a495fde8f82573c (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>
#include <string>

#include "base/callback_helpers.h"
#include "base/message_loop/message_loop.h"
#include "blimp/common/proto/blimp_message.pb.h"
#include "blimp/net/blimp_connection.h"
#include "blimp/net/common.h"
#include "blimp/net/connection_error_observer.h"
#include "blimp/net/test_common.h"
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::InSequence;
using testing::Return;
using testing::SaveArg;

namespace blimp {
namespace {

class BlimpConnectionTest : public testing::Test {
 public:
  BlimpConnectionTest() {
    scoped_ptr<testing::StrictMock<MockPacketWriter>> writer(
        new testing::StrictMock<MockPacketWriter>);
    writer_ = writer.get();
    connection_.reset(new BlimpConnection(make_scoped_ptr(new MockPacketReader),
                                          std::move(writer)));
    connection_->SetConnectionErrorObserver(&error_observer_);
  }

  ~BlimpConnectionTest() override {}

 protected:
  scoped_ptr<BlimpMessage> CreateInputMessage() {
    scoped_ptr<BlimpMessage> msg(new BlimpMessage);
    msg->set_type(BlimpMessage::INPUT);
    return msg;
  }

  scoped_ptr<BlimpMessage> CreateControlMessage() {
    scoped_ptr<BlimpMessage> msg(new BlimpMessage);
    msg->set_type(BlimpMessage::CONTROL);
    return msg;
  }

  base::MessageLoop message_loop_;
  testing::StrictMock<MockPacketWriter>* writer_;
  testing::StrictMock<MockConnectionErrorObserver> error_observer_;
  testing::StrictMock<MockBlimpMessageProcessor> receiver_;
  scoped_ptr<BlimpConnection> connection_;
};

// Write completes writing two packets asynchronously.
TEST_F(BlimpConnectionTest, AsyncTwoPacketsWrite) {
  net::CompletionCallback write_packet_cb;

  InSequence s;
  EXPECT_CALL(*writer_,
              WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
      .WillOnce(SaveArg<1>(&write_packet_cb))
      .RetiresOnSaturation();
  EXPECT_CALL(*writer_,
              WritePacket(BufferEqualsProto(*CreateControlMessage()), _))
      .WillOnce(SaveArg<1>(&write_packet_cb))
      .RetiresOnSaturation();

  BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
  net::TestCompletionCallback complete_cb_1;
  ASSERT_TRUE(write_packet_cb.is_null());
  sender->ProcessMessage(CreateInputMessage(),
                         complete_cb_1.callback());
  ASSERT_FALSE(write_packet_cb.is_null());
  base::ResetAndReturn(&write_packet_cb).Run(net::OK);
  EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());

  net::TestCompletionCallback complete_cb_2;
  ASSERT_TRUE(write_packet_cb.is_null());
  sender->ProcessMessage(CreateControlMessage(),
                         complete_cb_2.callback());
  ASSERT_FALSE(write_packet_cb.is_null());
  base::ResetAndReturn(&write_packet_cb).Run(net::OK);
  EXPECT_EQ(net::OK, complete_cb_2.WaitForResult());
}

// Writer completes writing two packets asynchronously.
// First write succeeds, second fails.
TEST_F(BlimpConnectionTest, AsyncTwoPacketsWriteWithError) {
  net::CompletionCallback write_packet_cb;

  InSequence s;
  EXPECT_CALL(*writer_,
              WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
      .WillOnce(SaveArg<1>(&write_packet_cb))
      .RetiresOnSaturation();
  EXPECT_CALL(*writer_,
              WritePacket(BufferEqualsProto(*CreateControlMessage()), _))
      .WillOnce(SaveArg<1>(&write_packet_cb))
      .RetiresOnSaturation();
  EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED));

  BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
  net::TestCompletionCallback complete_cb_1;
  sender->ProcessMessage(CreateInputMessage(),
                         complete_cb_1.callback());
  base::ResetAndReturn(&write_packet_cb).Run(net::OK);
  EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());

  net::TestCompletionCallback complete_cb_2;
  sender->ProcessMessage(CreateControlMessage(),
                         complete_cb_2.callback());
  base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED);
  EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult());
}

}  // namespace
}  // namespace blimp