github.com/mre-fog/trillianxx@v1.1.2-0.20180615153820-ae375a99d36a/docs/storage/commit_log/simkafka/kafka_test.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     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 simkafka
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	_ "github.com/golang/glog"
    22  )
    23  
    24  func TestReadEmpty(t *testing.T) {
    25  	topic := "test"
    26  	offset := 0
    27  	if got := Read(topic, offset); got != "" {
    28  		t.Errorf("Read(%q, %d)=%q; want ''", topic, offset, got)
    29  	}
    30  	offset = 2
    31  	if got := Read(topic, offset); got != "" {
    32  		t.Errorf("Read(%q, %d)=%q; want ''", topic, offset, got)
    33  	}
    34  	if got := ReadMultiple(topic, offset, 1); got != nil {
    35  		t.Errorf("ReadMultiple(%q, %d, 1)=%q; want ''", topic, offset, got)
    36  	}
    37  	if got, gotOffset := ReadLast(topic); got != "" || gotOffset != -1 {
    38  		t.Errorf("ReadLast(%q, %d)=%q,%d; want '',-1", topic, offset, got, gotOffset)
    39  	}
    40  }
    41  func TestRead(t *testing.T) {
    42  	topic := "test2"
    43  	want := "abc"
    44  	if got := Append(topic, want); got != 0 {
    45  		t.Errorf("Append(%q, %q)=%d; want 0", topic, want, got)
    46  	}
    47  	offset := 0
    48  	if got := Read(topic, offset); got != want {
    49  		t.Errorf("Read(%q, %d)=%q; want %q", topic, offset, got, want)
    50  	}
    51  	if got := ReadMultiple(topic, offset, 1); !reflect.DeepEqual(got, []string{want}) {
    52  		t.Errorf("Read(%q, %d)=%q; want [%q]", topic, offset, got, want)
    53  	}
    54  	if got := ReadMultiple(topic, offset, 10); !reflect.DeepEqual(got, []string{want}) {
    55  		t.Errorf("Read(%q, %d)=%q; want [%q]", topic, offset, got, want)
    56  	}
    57  	offset = 2
    58  	if got := Read(topic, offset); got != "" {
    59  		t.Errorf("Read(%q, %d)=%v; want ''", topic, offset, got)
    60  	}
    61  	if got := ReadMultiple(topic, offset, 1); got != nil {
    62  		t.Errorf("ReadMultiple(%q, %d, 1)=%v; want ''", topic, offset, got)
    63  	}
    64  }
    65  
    66  func TestAppend(t *testing.T) {
    67  	topic := "test3"
    68  	values := []string{"ab", "cd", "ef", "gh"}
    69  	for offset, value := range values {
    70  		if got := Append(topic, value); got != offset {
    71  			t.Errorf("Append(%q, %q)=%d; want %d", topic, value, got, offset)
    72  		}
    73  		if got, gotOffset := ReadLast("test3"); got != value || gotOffset != offset {
    74  			t.Errorf("ReadLast('test3')=%q,%d; want %q,%d", got, gotOffset, value, offset)
    75  		}
    76  	}
    77  	want := "| 0:ab | 1:cd | 2:ef | 3:gh |"
    78  	if got := topics["test3"].String(); got != want {
    79  		t.Errorf("topic.String()=%q; want %q", got, want)
    80  	}
    81  }