go.temporal.io/server@v1.23.0/common/persistence/persistencetest/queues.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package persistencetest
    26  
    27  import (
    28  	"context"
    29  	"testing"
    30  
    31  	"github.com/stretchr/testify/require"
    32  	"go.temporal.io/api/common/v1"
    33  	"go.temporal.io/api/enums/v1"
    34  
    35  	"go.temporal.io/server/common/persistence"
    36  	"go.temporal.io/server/service/history/tasks"
    37  )
    38  
    39  type (
    40  	getQueueKeyParams struct {
    41  		QueueType persistence.QueueV2Type
    42  		Category  tasks.Category
    43  	}
    44  )
    45  
    46  func WithQueueType(queueType persistence.QueueV2Type) func(p *getQueueKeyParams) {
    47  	return func(p *getQueueKeyParams) {
    48  		p.QueueType = queueType
    49  	}
    50  }
    51  
    52  func WithCategory(category tasks.Category) func(p *getQueueKeyParams) {
    53  	return func(p *getQueueKeyParams) {
    54  		p.Category = category
    55  	}
    56  }
    57  
    58  func GetQueueKey(t *testing.T, opts ...func(p *getQueueKeyParams)) persistence.QueueKey {
    59  	params := &getQueueKeyParams{
    60  		QueueType: persistence.QueueTypeHistoryNormal,
    61  		Category:  tasks.CategoryTransfer,
    62  	}
    63  	for _, opt := range opts {
    64  		opt(params)
    65  	}
    66  	// Note that it is important to include the test name in the cluster name to ensure that the generated queue name is
    67  	// unique across tests. That way, we can run many queue tests without any risk of queue name collisions.
    68  	return persistence.QueueKey{
    69  		QueueType:     params.QueueType,
    70  		Category:      params.Category,
    71  		SourceCluster: "test-source-cluster-" + t.Name(),
    72  		TargetCluster: "test-target-cluster-" + t.Name(),
    73  	}
    74  }
    75  
    76  type EnqueueParams struct {
    77  	Data         []byte
    78  	EncodingType int
    79  }
    80  
    81  func EnqueueMessage(
    82  	ctx context.Context,
    83  	queue persistence.QueueV2,
    84  	queueType persistence.QueueV2Type,
    85  	queueName string,
    86  	opts ...func(p *EnqueueParams),
    87  ) (*persistence.InternalEnqueueMessageResponse, error) {
    88  	params := EnqueueParams{
    89  		Data:         []byte("1"),
    90  		EncodingType: int(enums.ENCODING_TYPE_JSON),
    91  	}
    92  	for _, opt := range opts {
    93  		opt(&params)
    94  	}
    95  	return queue.EnqueueMessage(ctx, &persistence.InternalEnqueueMessageRequest{
    96  		QueueType: queueType,
    97  		QueueName: queueName,
    98  		Blob: &common.DataBlob{
    99  			EncodingType: enums.EncodingType(params.EncodingType),
   100  			Data:         params.Data,
   101  		},
   102  	})
   103  }
   104  
   105  func EnqueueMessagesForDelete(t *testing.T, q persistence.QueueV2, queueName string, queueType persistence.QueueV2Type) {
   106  	for i := 0; i < 2; i++ {
   107  		// We have to actually enqueue 2 messages. Otherwise, there won't be anything to actually delete,
   108  		// since we never delete the last message.
   109  		_, err := EnqueueMessage(context.Background(), q, queueType, queueName)
   110  		require.NoError(t, err)
   111  	}
   112  }