Repository: envoyproxy/envoy
File: test/common/stream_info/stream_info_impl_test.cc (C++)

Code before:
    auto ssl_info = std::make_shared<Ssl::MockConnectionInfo>();
    EXPECT_CALL(*ssl_info, sessionId()).WillRepeatedly(testing::ReturnRef(session_id));
    stream_info.upstreamInfo()->setUpstreamSslConnection(ssl_info);
    EXPECT_EQ(session_id, stream_info.upstreamInfo()->upstreamSslConnection()->sessionId());

    EXPECT_FALSE(stream_info.upstreamInfo()->upstreamConnectionId().has_value());
    stream_info.upstreamInfo()->setUpstreamConnectionId(12345);
    ASSERT_TRUE(stream_info.upstreamInfo()->upstreamConnectionId().has_value());
    EXPECT_EQ(12345, stream_info.upstreamInfo()->upstreamConnectionId().value());

    EXPECT_FALSE(stream_info.upstreamInfo()->upstreamInterfaceName().has_value());
    stream_info.upstreamInfo()->setUpstreamInterfaceName("lo");
    ASSERT_TRUE(stream_info.upstreamInfo()->upstreamInterfaceName().has_value());
    EXPECT_EQ("lo", stream_info.upstreamInfo()->upstreamInterfaceName().value());

    std::shared_ptr<UpstreamInfo> new_info = std::make_shared<UpstreamInfoImpl>();
    EXPECT_NE(stream_info.upstreamInfo(), new_info);
    stream_info.setUpstreamInfo(new_info);
    EXPECT_EQ(stream_info.upstreamInfo(), new_info);
  }
}

TEST_F(StreamInfoImplTest, CodecStreamId) {
  StreamInfoImpl stream_info(Http::Protocol::Http2, test_time_.timeSystem(), nullptr,
                             FilterState::LifeSpan::FilterChain);
  EXPECT_EQ(0, stream_info.codecStreamId());
  stream_info.setCodecStreamId(12345);
  EXPECT_EQ(12345, stream_info.codecStreamId());
}

TEST_F(StreamInfoImplTest, SetFromForRecreateStream) {
  StreamInfoImpl s1(Http::Protocol::Http2, test_time_.timeSystem(), nullptr,
                    FilterState::LifeSpan::FilterChain);

  s1.addBytesReceived(1);
  s1.downstreamTiming().onLastDownstreamRxByteReceived(test_time_.timeSystem());
  s1.addBytesRetransmitted(1);
  s1.addPacketsRetransmitted(1);

#ifdef __clang__
#if defined(__linux__)
#if defined(__has_feature) && !(__has_feature(thread_sanitizer))
  assertStreamInfoSize(s1);
#endif
#endif
#endif

  StreamInfoImpl s2(Http::Protocol::Http11, test_time_.timeSystem(), nullptr,
                    FilterState::LifeSpan::FilterChain);
  s2.setFromForRecreateStream(s1);
  EXPECT_EQ(s1.startTime(), s2.startTime());

Review comment (bug):
CI failure is legit, here should be updated to nullopt as well.

Code after:
    auto ssl_info = std::make_shared<Ssl::MockConnectionInfo>();
    EXPECT_CALL(*ssl_info, sessionId()).WillRepeatedly(testing::ReturnRef(session_id));
    stream_info.upstreamInfo()->setUpstreamSslConnection(ssl_info);
    EXPECT_EQ(session_id, stream_info.upstreamInfo()->upstreamSslConnection()->sessionId());

    EXPECT_FALSE(stream_info.upstreamInfo()->upstreamConnectionId().has_value());
    stream_info.upstreamInfo()->setUpstreamConnectionId(12345);
    ASSERT_TRUE(stream_info.upstreamInfo()->upstreamConnectionId().has_value());
    EXPECT_EQ(12345, stream_info.upstreamInfo()->upstreamConnectionId().value());

    EXPECT_FALSE(stream_info.upstreamInfo()->upstreamInterfaceName().has_value());
    stream_info.upstreamInfo()->setUpstreamInterfaceName("lo");
    ASSERT_TRUE(stream_info.upstreamInfo()->upstreamInterfaceName().has_value());
    EXPECT_EQ("lo", stream_info.upstreamInfo()->upstreamInterfaceName().value());

    std::shared_ptr<UpstreamInfo> new_info = std::make_shared<UpstreamInfoImpl>();
    EXPECT_NE(stream_info.upstreamInfo(), new_info);
    stream_info.setUpstreamInfo(new_info);
    EXPECT_EQ(stream_info.upstreamInfo(), new_info);
  }
}

TEST_F(StreamInfoImplTest, CodecStreamId) {
  StreamInfoImpl stream_info(Http::Protocol::Http2, test_time_.timeSystem(), nullptr,
                             FilterState::LifeSpan::FilterChain);
  EXPECT_EQ(absl::nullopt, stream_info.codecStreamId());
  stream_info.setCodecStreamId(12345);
  EXPECT_EQ(12345, stream_info.codecStreamId());
}

TEST_F(StreamInfoImplTest, SetFromForRecreateStream) {
  StreamInfoImpl s1(Http::Protocol::Http2, test_time_.timeSystem(), nullptr,
                    FilterState::LifeSpan::FilterChain);

  s1.addBytesReceived(1);
  s1.downstreamTiming().onLastDownstreamRxByteReceived(test_time_.timeSystem());
  s1.addBytesRetransmitted(1);
  s1.addPacketsRetransmitted(1);

#ifdef __clang__
#if defined(__linux__)
#if defined(__has_feature) && !(__has_feature(thread_sanitizer))
  assertStreamInfoSize(s1);
#endif
#endif
#endif

  StreamInfoImpl s2(Http::Protocol::Http11, test_time_.timeSystem(), nullptr,
                    FilterState::LifeSpan::FilterChain);
  s2.setFromForRecreateStream(s1);
  EXPECT_EQ(s1.startTime(), s2.startTime());