eintopf.info@v0.13.16/test/queue.go (about)

     1  // Copyright (C) 2023 The Eintopf authors
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  
    16  package test
    17  
    18  import (
    19  	"testing"
    20  
    21  	"eintopf.info/service/oqueue"
    22  	"github.com/google/go-cmp/cmp"
    23  	"github.com/google/go-cmp/cmp/cmpopts"
    24  )
    25  
    26  type TestQueue struct {
    27  	ops []oqueue.Operation
    28  }
    29  
    30  func NewTestQueue() *TestQueue {
    31  	return &TestQueue{
    32  		ops: []oqueue.Operation{},
    33  	}
    34  }
    35  
    36  func (q *TestQueue) AddOperation(op oqueue.Operation) error {
    37  	q.ops = append(q.ops, op)
    38  	return nil
    39  }
    40  
    41  func (q *TestQueue) WasCalled(t *testing.T, op oqueue.Operation) {
    42  	t.Helper()
    43  	if len(q.ops) == 0 {
    44  		t.Errorf("no operations")
    45  		return
    46  	}
    47  	op2 := q.ops[0]
    48  	if diff := cmp.Diff(op, op2, cmpopts.IgnoreUnexported(op)); diff != "" {
    49  		t.Errorf("operation mismatch (-want +got):\n%s", diff)
    50  	}
    51  	if op.UserID() != op2.UserID() {
    52  		t.Errorf("invalid userID: %s !== %s", op.UserID(), op2.UserID())
    53  	}
    54  	q.ops = q.ops[1:]
    55  }
    56  
    57  func (q *TestQueue) AddSubscriber(consume oqueue.OperationConsumer, worker int) {}
    58  
    59  func (q *TestQueue) Close() {}