...

Source file src/cloud.google.com/go/pubsub/internal/testutil/verifier.go

Documentation: cloud.google.com/go/pubsub/internal/testutil

     1  // Copyright 2019 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testutil
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  )
    22  
    23  // OrderedKeyMsg is a message with key and data.
    24  type OrderedKeyMsg struct {
    25  	Key  string
    26  	Data string
    27  }
    28  
    29  // VerifyKeyOrdering verifies that received data was published and received in
    30  // key order.
    31  //
    32  // TODO(deklerk): account for consistent redelivery.
    33  func VerifyKeyOrdering(publishData, receiveData []OrderedKeyMsg) error {
    34  	publishKeyMap := make(map[string][]string)
    35  	receiveKeyMap := make(map[string][]string)
    36  
    37  	for _, d := range publishData {
    38  		if d.Key == "" {
    39  			continue
    40  		}
    41  		publishKeyMap[d.Key] = append(publishKeyMap[d.Key], d.Data)
    42  	}
    43  
    44  	for _, d := range receiveData {
    45  		if d.Key == "" {
    46  			continue
    47  		}
    48  		receiveKeyMap[d.Key] = append(receiveKeyMap[d.Key], d.Data)
    49  	}
    50  
    51  	if len(publishKeyMap) != len(receiveKeyMap) {
    52  		return fmt.Errorf("published %d keys, received %d - expected them to be equal but they were not", len(publishKeyMap), len(receiveKeyMap))
    53  	}
    54  
    55  	for k, pb := range publishKeyMap {
    56  		rd, ok := receiveKeyMap[k]
    57  		if !ok {
    58  			// Should never happen unless we're using a topic that's got some
    59  			// data in it already / topic test pollution / etc. So, basically,
    60  			// make sure a fresh topic is always used for integration tests.
    61  			return fmt.Errorf("saw key %s, but we never published this key", k)
    62  		}
    63  
    64  		if diff := cmp.Diff(rd, pb); diff != "" {
    65  			return fmt.Errorf("%s: got -, want +\n\t%s", k, diff)
    66  		}
    67  	}
    68  
    69  	return nil
    70  }
    71  

View as plain text