test/common/config/grpc_mux_impl_test.cc
// Validate behavior when watches specify resources (potentially overlapping).
Validate behavior when watches specify resources (potentially overlapping).
Validate behavior when watches specify resources ( potentially overlapping ) .
TEST_F(GrpcMuxImplTest, WatchDemux) {
  setup();
  InSequence s;
  const std::string& type_url = Config::TypeUrl::get().ClusterLoadAssignment;
  NiceMock<MockGrpcMuxCallbacks> foo_callbacks;
  auto foo_sub = grpc_mux_->subscribe(type_url, {"x", "y"}, foo_callbacks);
  NiceMock<MockGrpcMuxCallbacks> bar_callbacks;
  auto bar_sub = grpc_mux_->subscribe(type_url, {"y", "z"}, bar_callbacks);
  EXPECT_CALL(*async_client_, start(_, _)).WillOnce(Return(&async_stream_));
  expectSendMessage(type_url, {"y", "z", "x"}, "");
  grpc_mux_->start();
  {
    std::unique_ptr<envoy::api::v2::DiscoveryResponse> response(
        new envoy::api::v2::DiscoveryResponse());
    response->set_type_url(type_url);
    response->set_version_info("1");
    envoy::api::v2::ClusterLoadAssignment load_assignment;
    load_assignment.set_cluster_name("x");
    response->add_resources()->PackFrom(load_assignment);
    EXPECT_CALL(bar_callbacks, onConfigUpdate(_, "1"))
        .WillOnce(Invoke([](const Protobuf::RepeatedPtrField<ProtobufWkt::Any>& resources,
                            const std::string&) { EXPECT_TRUE(resources.empty()); }));
    EXPECT_CALL(foo_callbacks, onConfigUpdate(_, "1"))
        .WillOnce(
            Invoke([&load_assignment](const Protobuf::RepeatedPtrField<ProtobufWkt::Any>& resources,
                                      const std::string&) {
              EXPECT_EQ(1, resources.size());
              envoy::api::v2::ClusterLoadAssignment expected_assignment;
              resources[0].UnpackTo(&expected_assignment);
              EXPECT_TRUE(TestUtility::protoEqual(expected_assignment, load_assignment));
            }));
    expectSendMessage(type_url, {"y", "z", "x"}, "1");
    grpc_mux_->onReceiveMessage(std::move(response));
  }
  {
    std::unique_ptr<envoy::api::v2::DiscoveryResponse> response(
        new envoy::api::v2::DiscoveryResponse());
    response->set_type_url(type_url);
    response->set_version_info("2");
    envoy::api::v2::ClusterLoadAssignment load_assignment_x;
    load_assignment_x.set_cluster_name("x");
    response->add_resources()->PackFrom(load_assignment_x);
    envoy::api::v2::ClusterLoadAssignment load_assignment_y;
    load_assignment_y.set_cluster_name("y");
    response->add_resources()->PackFrom(load_assignment_y);
    envoy::api::v2::ClusterLoadAssignment load_assignment_z;
    load_assignment_z.set_cluster_name("z");
    response->add_resources()->PackFrom(load_assignment_z);
    EXPECT_CALL(bar_callbacks, onConfigUpdate(_, "2"))
        .WillOnce(Invoke(
            [&load_assignment_y, &load_assignment_z](
                const Protobuf::RepeatedPtrField<ProtobufWkt::Any>& resources, const std::string&) {
              EXPECT_EQ(2, resources.size());
              envoy::api::v2::ClusterLoadAssignment expected_assignment;
              resources[0].UnpackTo(&expected_assignment);
              EXPECT_TRUE(TestUtility::protoEqual(expected_assignment, load_assignment_y));
              resources[1].UnpackTo(&expected_assignment);
              EXPECT_TRUE(TestUtility::protoEqual(expected_assignment, load_assignment_z));
            }));
    EXPECT_CALL(foo_callbacks, onConfigUpdate(_, "2"))
        .WillOnce(Invoke(
            [&load_assignment_x, &load_assignment_y](
                const Protobuf::RepeatedPtrField<ProtobufWkt::Any>& resources, const std::string&) {
              EXPECT_EQ(2, resources.size());
              envoy::api::v2::ClusterLoadAssignment expected_assignment;
              resources[0].UnpackTo(&expected_assignment);
              EXPECT_TRUE(TestUtility::protoEqual(expected_assignment, load_assignment_x));
              resources[1].UnpackTo(&expected_assignment);
              EXPECT_TRUE(TestUtility::protoEqual(expected_assignment, load_assignment_y));
            }));
    expectSendMessage(type_url, {"y", "z", "x"}, "2");
    grpc_mux_->onReceiveMessage(std::move(response));
  }
  expectSendMessage(type_url, {"x", "y"}, "2");
  expectSendMessage(type_url, {}, "2");
}
TEST_F ( GrpcMuxImplTest , WatchDemux ) { setup ( ) ; InSequence s ; const std :: string & type_url = Config :: TypeUrl :: get ( ) . ClusterLoadAssignment ; NiceMock < MockGrpcMuxCallbacks > foo_callbacks ; auto foo_sub = grpc_mux_ -> subscribe ( type_url , { " " , " " } , foo_callbacks ) ; NiceMock < MockGrpcMuxCallbacks > bar_callbacks ; auto bar_sub = grpc_mux_ -> subscribe ( type_url , { " " , " " } , bar_callbacks ) ; EXPECT_CALL ( * async_client_ , start ( _ , _ ) ) . WillOnce ( Return ( & async_stream_ ) ) ; expectSendMessage ( type_url , { " " , " " , " " } , " " ) ; grpc_mux_ -> start ( ) ; { std :: unique_ptr < envoy :: api :: v2 :: DiscoveryResponse > response ( new envoy :: api :: v2 :: DiscoveryResponse ( ) ) ; response -> set_type_url ( type_url ) ; response -> set_version_info ( " " ) ; envoy :: api :: v2 :: ClusterLoadAssignment load_assignment ; load_assignment . set_cluster_name ( " " ) ; response -> add_resources ( ) -> PackFrom ( load_assignment ) ; EXPECT_CALL ( bar_callbacks , onConfigUpdate ( _ , " " ) ) . WillOnce ( Invoke ( [ ] ( const Protobuf :: RepeatedPtrField < ProtobufWkt :: Any > & resources , const std :: string & ) { EXPECT_TRUE ( resources . empty ( ) ) ; } ) ) ; EXPECT_CALL ( foo_callbacks , onConfigUpdate ( _ , " " ) ) . WillOnce ( Invoke ( [ & load_assignment ] ( const Protobuf :: RepeatedPtrField < ProtobufWkt :: Any > & resources , const std :: string & ) { EXPECT_EQ ( 1 , resources . size ( ) ) ; envoy :: api :: v2 :: ClusterLoadAssignment expected_assignment ; resources [ 0 ] . UnpackTo ( & expected_assignment ) ; EXPECT_TRUE ( TestUtility :: protoEqual ( expected_assignment , load_assignment ) ) ; } ) ) ; expectSendMessage ( type_url , { " " , " " , " " } , " " ) ; grpc_mux_ -> onReceiveMessage ( std :: move ( response ) ) ; } { std :: unique_ptr < envoy :: api :: v2 :: DiscoveryResponse > response ( new envoy :: api :: v2 :: DiscoveryResponse ( ) ) ; response -> set_type_url ( type_url ) ; response -> set_version_info ( " " ) ; envoy :: api :: v2 :: ClusterLoadAssignment load_assignment_x ; load_assignment_x . set_cluster_name ( " " ) ; response -> add_resources ( ) -> PackFrom ( load_assignment_x ) ; envoy :: api :: v2 :: ClusterLoadAssignment load_assignment_y ; load_assignment_y . set_cluster_name ( " " ) ; response -> add_resources ( ) -> PackFrom ( load_assignment_y ) ; envoy :: api :: v2 :: ClusterLoadAssignment load_assignment_z ; load_assignment_z . set_cluster_name ( " " ) ; response -> add_resources ( ) -> PackFrom ( load_assignment_z ) ; EXPECT_CALL ( bar_callbacks , onConfigUpdate ( _ , " " ) ) . WillOnce ( Invoke ( [ & load_assignment_y , & load_assignment_z ] ( const Protobuf :: RepeatedPtrField < ProtobufWkt :: Any > & resources , const std :: string & ) { EXPECT_EQ ( 2 , resources . size ( ) ) ; envoy :: api :: v2 :: ClusterLoadAssignment expected_assignment ; resources [ 0 ] . UnpackTo ( & expected_assignment ) ; EXPECT_TRUE ( TestUtility :: protoEqual ( expected_assignment , load_assignment_y ) ) ; resources [ 1 ] . UnpackTo ( & expected_assignment ) ; EXPECT_TRUE ( TestUtility :: protoEqual ( expected_assignment , load_assignment_z ) ) ; } ) ) ; EXPECT_CALL ( foo_callbacks , onConfigUpdate ( _ , " " ) ) . WillOnce ( Invoke ( [ & load_assignment_x , & load_assignment_y ] ( const Protobuf :: RepeatedPtrField < ProtobufWkt :: Any > & resources , const std :: string & ) { EXPECT_EQ ( 2 , resources . size ( ) ) ; envoy :: api :: v2 :: ClusterLoadAssignment expected_assignment ; resources [ 0 ] . UnpackTo ( & expected_assignment ) ; EXPECT_TRUE ( TestUtility :: protoEqual ( expected_assignment , load_assignment_x ) ) ; resources [ 1 ] . UnpackTo ( & expected_assignment ) ; EXPECT_TRUE ( TestUtility :: protoEqual ( expected_assignment , load_assignment_y ) ) ; } ) ) ; expectSendMessage ( type_url , { " " , " " , " " } , " " ) ; grpc_mux_ -> onReceiveMessage ( std :: move ( response ) ) ; } expectSendMessage ( type_url , { " " , " " } , " " ) ; expectSendMessage ( type_url , { } , " " ) ; }
Validate behavior when watches specify resources (potentially overlapping).
Validate behavior when watches specify resources ( potentially overlapping ) .
// Should dedupe the "x" resource.