1// Copyright (c) 2014 Couchbase, Inc. 2// Licensed under the Apache License, Version 2.0 (the "License"); 3// you may not use this file except in compliance with the 4// License. You may obtain a copy of the License at 5// http://www.apache.org/licenses/LICENSE-2.0 6// Unless required by applicable law or agreed to in writing, 7// software distributed under the License is distributed on an "AS 8// IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 9// express or implied. See the License for the specific language 10// governing permissions and limitations under the License. 11 12package trace 13 14import ( 15 "reflect" 16 "testing" 17) 18 19func TestTrace(t *testing.T) { 20 r := NewRingBuffer(2, nil) 21 if r.Cap() != 2 { 22 t.Errorf("expected 2") 23 } 24 25 m := r.Msgs() 26 exp := []Msg{} 27 if !reflect.DeepEqual(m, exp) { 28 t.Errorf("expected %#v, got %#v", exp, m) 29 } 30 31 r.Add("hi", nil) 32 m = r.Msgs() 33 exp = []Msg{ 34 Msg{"hi", nil, 1}, 35 } 36 if !reflect.DeepEqual(m, exp) { 37 t.Errorf("expected %#v, got %#v", exp, m) 38 } 39 40 r.Add("bye", nil) 41 m = r.Msgs() 42 exp = []Msg{ 43 Msg{"hi", nil, 1}, 44 Msg{"bye", nil, 1}, 45 } 46 if !reflect.DeepEqual(m, exp) { 47 t.Errorf("expected %#v, got %#v", exp, m) 48 } 49 50 r.Add("buh", nil) 51 m = r.Msgs() 52 exp = []Msg{ 53 Msg{"bye", nil, 1}, 54 Msg{"buh", nil, 1}, 55 } 56 if !reflect.DeepEqual(m, exp) { 57 t.Errorf("expected %#v, got %#v", exp, m) 58 } 59 60 r.Add("buh", nil) 61 m = r.Msgs() 62 exp = []Msg{ 63 Msg{"buh", nil, 1}, 64 Msg{"buh", nil, 1}, 65 } 66 if !reflect.DeepEqual(m, exp) { 67 t.Errorf("expected %#v, got %#v", exp, m) 68 } 69 70 if !reflect.DeepEqual(r.Last(), &Msg{"buh", nil, 1}) { 71 t.Errorf("expected last to be buh") 72 } 73 74 s := MsgsToString(r.Msgs(), "\n", "") 75 exps := "buh\nbuh" 76 if s != exps { 77 t.Errorf("expected string %q, got %q", exps, s) 78 } 79 80 s = MsgsToString(r.Msgs(), "\n", "foo") 81 exps = "buh\nfoobuh" 82 if s != exps { 83 t.Errorf("expected string %q, got %q", exps, s) 84 } 85} 86 87func TestTraceConsolidateByTitle(t *testing.T) { 88 r := NewRingBuffer(2, ConsolidateByTitle) 89 if r.Cap() != 2 { 90 t.Errorf("expected 2") 91 } 92 93 m := r.Msgs() 94 exp := []Msg{} 95 if !reflect.DeepEqual(m, exp) { 96 t.Errorf("expected %#v, got %#v", exp, m) 97 } 98 99 r.Add("hi", nil) 100 m = r.Msgs() 101 exp = []Msg{ 102 Msg{"hi", nil, 1}, 103 } 104 if !reflect.DeepEqual(m, exp) { 105 t.Errorf("expected %#v, got %#v", exp, m) 106 } 107 108 r.Add("hi", nil) 109 m = r.Msgs() 110 exp = []Msg{ 111 Msg{"hi", nil, 2}, 112 } 113 if !reflect.DeepEqual(m, exp) { 114 t.Errorf("expected %#v, got %#v", exp, m) 115 } 116 117 r.Add("hi", nil) 118 m = r.Msgs() 119 exp = []Msg{ 120 Msg{"hi", nil, 3}, 121 } 122 if !reflect.DeepEqual(m, exp) { 123 t.Errorf("expected %#v, got %#v", exp, m) 124 } 125 126 r.Add("bye", nil) 127 m = r.Msgs() 128 exp = []Msg{ 129 Msg{"hi", nil, 3}, 130 Msg{"bye", nil, 1}, 131 } 132 if !reflect.DeepEqual(m, exp) { 133 t.Errorf("expected %#v, got %#v", exp, m) 134 } 135 136 r.Add("bye", nil) 137 m = r.Msgs() 138 exp = []Msg{ 139 Msg{"hi", nil, 3}, 140 Msg{"bye", nil, 2}, 141 } 142 if !reflect.DeepEqual(m, exp) { 143 t.Errorf("expected %#v, got %#v", exp, m) 144 } 145 146 r.Add("buh", nil) 147 m = r.Msgs() 148 exp = []Msg{ 149 Msg{"bye", nil, 2}, 150 Msg{"buh", nil, 1}, 151 } 152 if !reflect.DeepEqual(m, exp) { 153 t.Errorf("expected %#v, got %#v", exp, m) 154 } 155 156 r.Add("buh", nil) 157 m = r.Msgs() 158 exp = []Msg{ 159 Msg{"bye", nil, 2}, 160 Msg{"buh", nil, 2}, 161 } 162 if !reflect.DeepEqual(m, exp) { 163 t.Errorf("expected %#v, got %#v", exp, m) 164 } 165 166 if !reflect.DeepEqual(r.Last(), &Msg{"buh", nil, 2}) { 167 t.Errorf("expected last to be buh") 168 } 169 170 s := MsgsToString(r.Msgs(), "\n", "") 171 exps := "bye (2x)\nbuh (2x)" 172 if s != exps { 173 t.Errorf("expected string %q, got %q", exps, s) 174 } 175 176 s = MsgsToString(r.Msgs(), "\n", "prefix") 177 exps = "bye (2x)\nprefixbuh (2x)" 178 if s != exps { 179 t.Errorf("expected string %q, got %q", exps, s) 180 } 181} 182