Best Go-testdeep code snippet using test.LastMessage
module_test.go
Source:module_test.go
...42 log.DefaultLogWriter(mockWriter)43 log.DefaultLogFormatter(formatter.NewDefaultFormatter(false))44 log.DefaultWithFileLine(true)45 log.Debug("hello")46 assert.Regexp(t, regexp.MustCompile(`"#file"`), mockWriter.LastMessage)47 assert.Regexp(t, regexp.MustCompile(`"#line"`), mockWriter.LastMessage)48 assert.Regexp(t, regexp.MustCompile(`"#package"`), mockWriter.LastMessage)49 // log.DefaultWithFileLine(false)50 log.Default().WithFileLine(false)51 log.Debug("hello")52 assert.NotRegexp(t, regexp.MustCompile(`"#file"`), mockWriter.LastMessage)53 assert.NotRegexp(t, regexp.MustCompile(`"#line"`), mockWriter.LastMessage)54 assert.NotRegexp(t, regexp.MustCompile(`"#package"`), mockWriter.LastMessage)55 log.Info("hello")56 assert.NotRegexp(t, regexp.MustCompile(`"#file"`), mockWriter.LastMessage)57 assert.NotRegexp(t, regexp.MustCompile(`"#line"`), mockWriter.LastMessage)58 assert.NotRegexp(t, regexp.MustCompile(`"#package"`), mockWriter.LastMessage)59}60func TestDefaultLocation(t *testing.T) {61 log.Reset()62 mockWriter := &MockWriter{}63 log.DefaultLogWriter(mockWriter)64 log.DefaultLogFormatter(formatter.NewDefaultFormatter(false))65 // testing UTC66 loc, _ := time.LoadLocation("UTC")67 log.DefaultLocation(loc)68 log.Warning("hello")69 logTimeStr := strings.Trim(strings.Split(mockWriter.LastMessage, " ")[0], "[]")70 realTimeStr := time.Now().In(loc).Format(time.RFC3339)71 assert.Equal(t, logTimeStr[:13], realTimeStr[:13])72 // testing Local73 loc, _ = time.LoadLocation("Local")74 // log.DefaultLocation(loc)75 log.Default().Location(loc)76 log.Warning("hello")77 logTimeStr = strings.Trim(strings.Split(mockWriter.LastMessage, " ")[0], "[]")78 realTimeStr = time.Now().In(loc).Format(time.RFC3339)79 assert.Equal(t, logTimeStr[:13], realTimeStr[:13])80}81func TestDefaultLogLevel(t *testing.T) {82 log.Reset()83 mockWriter := &MockWriter{}84 log.DefaultLogWriter(mockWriter)85 log.DefaultLogFormatter(formatter.NewDefaultFormatter(false))86 log.DefaultLogLevel(level.Warning)87 log.Debug("hello")88 assert.Equal(t, level.Level(0), mockWriter.LastLevel)89 log.Info("hello")90 assert.Equal(t, level.Level(0), mockWriter.LastLevel)91 log.Warning("hello")92 assert.Equal(t, level.Warning, mockWriter.LastLevel)93 log.Error("hello")94 assert.Equal(t, level.Error, mockWriter.LastLevel)95}96func TestLogger_Location(t *testing.T) {97 log.Reset()98 mockWriter := &MockWriter{}99 logger := log.Module("test").100 Writer(mockWriter).101 Formatter(formatter.NewDefaultFormatter(false))102 // testing UTC103 loc, _ := time.LoadLocation("UTC")104 logger.Location(loc)105 logger.Warning("hello")106 logTimeStr := strings.Trim(strings.Split(mockWriter.LastMessage, " ")[0], "[]")107 realTimeStr := time.Now().In(loc).Format(time.RFC3339)108 assert.Equal(t, logTimeStr[:13], realTimeStr[:13])109 // testing Local110 loc, _ = time.LoadLocation("Local")111 logger.Location(loc)112 logger.Warning("hello")113 logTimeStr = strings.Trim(strings.Split(mockWriter.LastMessage, " ")[0], "[]")114 realTimeStr = time.Now().In(loc).Format(time.RFC3339)115 assert.Equal(t, logTimeStr[:13], realTimeStr[:13])116}117func TestLogger_WithFileLine(t *testing.T) {118 log.Reset()119 mockWriter := &MockWriter{}120 logger := log.Module("test").121 Writer(mockWriter).122 Formatter(formatter.NewDefaultFormatter(false))123 logger.WithFileLine(true)124 logger.Debug("hello")125 assert.Regexp(t, regexp.MustCompile(`"#file"`), mockWriter.LastMessage)126 assert.Regexp(t, regexp.MustCompile(`"#line"`), mockWriter.LastMessage)127 assert.Regexp(t, regexp.MustCompile(`"#package"`), mockWriter.LastMessage)128 logger.WithFileLine(false)129 logger.Debug("hello")130 assert.NotRegexp(t, regexp.MustCompile(`"#file"`), mockWriter.LastMessage)131 assert.NotRegexp(t, regexp.MustCompile(`"#line"`), mockWriter.LastMessage)132 assert.NotRegexp(t, regexp.MustCompile(`"#package"`), mockWriter.LastMessage)133 logger.Info("hello")134 assert.NotRegexp(t, regexp.MustCompile(`"#file"`), mockWriter.LastMessage)135 assert.NotRegexp(t, regexp.MustCompile(`"#line"`), mockWriter.LastMessage)136 assert.NotRegexp(t, regexp.MustCompile(`"#package"`), mockWriter.LastMessage)137}138func TestModuleLogger(t *testing.T) {139 log.Reset()140 mockWriter := &MockWriter{}141 log.DefaultLogWriter(mockWriter)142 log.DefaultLogFormatter(formatter.NewDefaultFormatter(false))143 var logger = log.Module("test")144 logger.Emergency("hello")145 assert.Equal(t, level.Emergency, mockWriter.LastLevel)146 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {.*?}", level.Emergency.GetLevelName())), mockWriter.LastMessage)147 logger.Alert("hello")148 assert.Equal(t, level.Alert, mockWriter.LastLevel)149 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {.*?}", level.Alert.GetLevelName())), mockWriter.LastMessage)150 logger.Critical("hello")151 assert.Equal(t, level.Critical, mockWriter.LastLevel)152 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {.*?}", level.Critical.GetLevelName())), mockWriter.LastMessage)153 logger.Error("hello")154 assert.Equal(t, level.Error, mockWriter.LastLevel)155 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {.*?}", level.Error.GetLevelName())), mockWriter.LastMessage)156 logger.Warning("hello")157 assert.Equal(t, level.Warning, mockWriter.LastLevel)158 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {}", level.Warning.GetLevelName())), mockWriter.LastMessage)159 logger.Notice("hello")160 assert.Equal(t, level.Notice, mockWriter.LastLevel)161 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {}", level.Notice.GetLevelName())), mockWriter.LastMessage)162 logger.Info("hello")163 assert.Equal(t, level.Info, mockWriter.LastLevel)164 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {}", level.Info.GetLevelName())), mockWriter.LastMessage)165 logger.Debug("hello")166 assert.Equal(t, level.Debug, mockWriter.LastLevel)167 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {.*?}", level.Debug.GetLevelName())), mockWriter.LastMessage)168 // logf169 logger.Emergencyf("hello %s", "world")170 assert.Equal(t, level.Emergency, mockWriter.LastLevel)171 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {.*?}", level.Emergency.GetLevelName())), mockWriter.LastMessage)172 logger.Alertf("hello %s", "world")173 assert.Equal(t, level.Alert, mockWriter.LastLevel)174 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {.*?}", level.Alert.GetLevelName())), mockWriter.LastMessage)175 logger.Criticalf("hello %s", "world")176 assert.Equal(t, level.Critical, mockWriter.LastLevel)177 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {.*?}", level.Critical.GetLevelName())), mockWriter.LastMessage)178 logger.Errorf("hello %s", "world")179 assert.Equal(t, level.Error, mockWriter.LastLevel)180 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {.*?}", level.Error.GetLevelName())), mockWriter.LastMessage)181 logger.Warningf("hello %s", "world")182 assert.Equal(t, level.Warning, mockWriter.LastLevel)183 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {}", level.Warning.GetLevelName())), mockWriter.LastMessage)184 logger.Noticef("hello %s", "world")185 assert.Equal(t, level.Notice, mockWriter.LastLevel)186 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {}", level.Notice.GetLevelName())), mockWriter.LastMessage)187 logger.Infof("hello %s", "world")188 assert.Equal(t, level.Info, mockWriter.LastLevel)189 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {}", level.Info.GetLevelName())), mockWriter.LastMessage)190 logger.Debugf("hello %s", "world")191 assert.Equal(t, level.Debug, mockWriter.LastLevel)192 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {.*?}", level.Debug.GetLevelName())), mockWriter.LastMessage)193 logger.Print("hello")194 assert.Equal(t, level.Debug, mockWriter.LastLevel)195 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {.*?}", level.Debug.GetLevelName())), mockWriter.LastMessage)196}197func TestAll_Writer(t *testing.T) {198 log.Reset()199 var module1 = log.Module("module1")200 var module2 = log.Module("module2")201 mockWriter := &MockWriter{}202 log.All().LogWriter(mockWriter)203 log.All().LogFormatter(formatter.NewJSONFormatter())204 log.Alert("Hello, world")205 module1.Debug("Hello, world")206 module1.Info("Hello, world")207 module2.Error("Hello, world")208 module2.Warning("Hello, world")209 assert.EqualValues(t, 5, mockWriter.WriteCount)210 assert.Regexp(t, regexp.MustCompile(`^{.*?}$`), mockWriter.LastMessage)211}212func TestAll_WithFileLine(t *testing.T) {213 log.Reset()214 mockWriter := &MockWriter{}215 log.DefaultLogWriter(mockWriter)216 log.DefaultLogFormatter(formatter.NewDefaultFormatter(false))217 var module1 = log.Module("module1")218 log.All().WithFileLine(true)219 log.Debug("hello")220 assert.Regexp(t, regexp.MustCompile(`"#file"`), mockWriter.LastMessage)221 assert.Regexp(t, regexp.MustCompile(`"#line"`), mockWriter.LastMessage)222 assert.Regexp(t, regexp.MustCompile(`"#package"`), mockWriter.LastMessage)223 log.All().WithFileLine(false)224 log.Debug("hello")225 assert.NotRegexp(t, regexp.MustCompile(`"#file"`), mockWriter.LastMessage)226 assert.NotRegexp(t, regexp.MustCompile(`"#line"`), mockWriter.LastMessage)227 assert.NotRegexp(t, regexp.MustCompile(`"#package"`), mockWriter.LastMessage)228 log.Info("hello")229 assert.NotRegexp(t, regexp.MustCompile(`"#file"`), mockWriter.LastMessage)230 assert.NotRegexp(t, regexp.MustCompile(`"#line"`), mockWriter.LastMessage)231 assert.NotRegexp(t, regexp.MustCompile(`"#package"`), mockWriter.LastMessage)232 log.All().WithFileLine(true)233 module1.Debug("hello")234 assert.Regexp(t, regexp.MustCompile(`"#file"`), mockWriter.LastMessage)235 assert.Regexp(t, regexp.MustCompile(`"#line"`), mockWriter.LastMessage)236 assert.Regexp(t, regexp.MustCompile(`"#package"`), mockWriter.LastMessage)237 log.All().WithFileLine(false)238 module1.Debug("hello")239 assert.NotRegexp(t, regexp.MustCompile(`"#file"`), mockWriter.LastMessage)240 assert.NotRegexp(t, regexp.MustCompile(`"#line"`), mockWriter.LastMessage)241 assert.NotRegexp(t, regexp.MustCompile(`"#package"`), mockWriter.LastMessage)242 module1.Info("hello")243 assert.NotRegexp(t, regexp.MustCompile(`"#file"`), mockWriter.LastMessage)244 assert.NotRegexp(t, regexp.MustCompile(`"#line"`), mockWriter.LastMessage)245 assert.NotRegexp(t, regexp.MustCompile(`"#package"`), mockWriter.LastMessage)246}247func TestAll_Location(t *testing.T) {248 log.Reset()249 mockWriter := &MockWriter{}250 log.DefaultLogWriter(mockWriter)251 log.DefaultLogFormatter(formatter.NewDefaultFormatter(false))252 // testing UTC253 loc, _ := time.LoadLocation("UTC")254 log.All().Location(loc)255 log.Warning("hello")256 logTimeStr := strings.Trim(strings.Split(mockWriter.LastMessage, " ")[0], "[]")257 realTimeStr := time.Now().In(loc).Format(time.RFC3339)258 assert.Equal(t, logTimeStr[:13], realTimeStr[:13])259 // testing Local260 loc, _ = time.LoadLocation("Local")261 // log.DefaultLocation(loc)262 log.All().Location(loc)263 log.Warning("hello")264 logTimeStr = strings.Trim(strings.Split(mockWriter.LastMessage, " ")[0], "[]")265 realTimeStr = time.Now().In(loc).Format(time.RFC3339)266 assert.Equal(t, logTimeStr[:13], realTimeStr[:13])267}268func TestAll_LogLevel(t *testing.T) {269 log.Reset()270 mockWriter := &MockWriter{}271 log.DefaultLogWriter(mockWriter)272 log.DefaultLogFormatter(formatter.NewDefaultFormatter(false))273 mod1 := log.Module("hello")274 log.All().LogLevel(level.Warning)275 log.Debug("hello")276 assert.Equal(t, level.Level(0), mockWriter.LastLevel)277 log.Info("hello")278 assert.Equal(t, level.Level(0), mockWriter.LastLevel)279 log.Warning("hello")280 assert.Equal(t, level.Warning, mockWriter.LastLevel)281 log.Error("hello")282 assert.Equal(t, level.Error, mockWriter.LastLevel)283 mod1.Debug("hello")284 assert.Equal(t, level.Error, mockWriter.LastLevel)285 mod1.Warning("hello")286 assert.Equal(t, level.Warning, mockWriter.LastLevel)287}288func TestALl_DynamicModuleName(t *testing.T) {289 log.Reset()290 mockWriter := &MockWriter{}291 log.DefaultLogWriter(mockWriter)292 log.DefaultLogFormatter(formatter.NewJSONFormatter())293 log.All().DynamicModuleName(false)294 log.Error("Hello, world")295 assert.Equal(t, "main", jsoniter.Get([]byte(mockWriter.LastMessage), "module").ToString())296 log.All().DynamicModuleName(true)297 log.Debug("hello, world")298 assert.Equal(t, "github.com.mylxsw.asteria.log_test", jsoniter.Get([]byte(mockWriter.LastMessage), "module").ToString())299}300func TestDefaultDynamicModuleName(t *testing.T) {301 log.Reset()302 mockWriter := &MockWriter{}303 log.DefaultLogWriter(mockWriter)304 log.DefaultLogFormatter(formatter.NewJSONFormatter())305 log.DefaultDynamicModuleName(true)306 log.Debug("Hello, world")307 assert.Equal(t, "github.com.mylxsw.asteria.log_test", jsoniter.Get([]byte(mockWriter.LastMessage), "module").ToString())308}309func TestLogger_DynamicModuleName(t *testing.T) {310 log.Reset()311 mockWriter := &MockWriter{}312 log.DefaultLogWriter(mockWriter)313 log.DefaultLogFormatter(formatter.NewJSONFormatter())314 log.Default().DynamicModuleName(true)315 log.Debug("Hello")316 assert.Equal(t, "github.com.mylxsw.asteria.log_test", jsoniter.Get([]byte(mockWriter.LastMessage), "module").ToString())317 log.Module("test").Debug("Hello")318 assert.Equal(t, "test", jsoniter.Get([]byte(mockWriter.LastMessage), "module").ToString())319}320func TestLoggers_LogLevel(t *testing.T) {321 log.Reset()322 var cases = map[level.Level][]int{323 level.Debug: {1, 1, 1, 1, 1, 1, 1, 1},324 level.Info: {0, 1, 1, 1, 1, 1, 1, 1},325 level.Notice: {0, 0, 1, 1, 1, 1, 1, 1},326 level.Warning: {0, 0, 0, 1, 1, 1, 1, 1},327 level.Error: {0, 0, 0, 0, 1, 1, 1, 1},328 level.Critical: {0, 0, 0, 0, 0, 1, 1, 1},329 level.Alert: {0, 0, 0, 0, 0, 0, 1, 1},330 level.Emergency: {0, 0, 0, 0, 0, 0, 0, 1},331 }332 for le, cas := range cases {...
message_receiver_test.go
Source:message_receiver_test.go
1package protocol2import (3 "fmt"4 "github.com/sirupsen/logrus"5 "testing"6 "ups/sp/server/protocol/def"7 "ups/sp/server/protocol/impl"8)9type SimpleOutput struct {10 lastMessage def.Message11 messages []def.Message12}13func (s *SimpleOutput) Read(message def.Message) def.Response {14 s.lastMessage = message15 s.messages = append(s.messages, message)16 return impl.SuccessResponse("test")17}18func TestReceive(t *testing.T) {19 jsonReader := SimpleOutput{}20 smr := impl.SimpleTcpMessageReceiver{21 TestMode: true,22 }23 smr.SetMessageReader(&jsonReader)24 sent := "$10#5#10#Hello guys"25 bytes := []byte(sent)26 smr.Receive(1, bytes, len(bytes))27 if jsonReader.lastMessage.Content() != "Hello guys" {28 t.Errorf("Got message %s, want %s", jsonReader.lastMessage.Content(), "Hello guys")29 } else if jsonReader.lastMessage.Type() != 5 {30 t.Errorf("Got message type %d, want %d", jsonReader.lastMessage.Type(), 5)31 } else if jsonReader.lastMessage.ClientID() != 1 {32 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ClientID(), 1)33 } else if jsonReader.lastMessage.ID() != 10 {34 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ID(), 10)35 }36}37func TestReceive3(t *testing.T) {38 jsonReader := SimpleOutput{}39 smr := impl.SimpleTcpMessageReceiver{40 TestMode: true,41 }42 smr.SetMessageReader(&jsonReader)43 sent := "$12#10#hELLO$10#5#10#Hello guys"44 bytes := []byte(sent)45 smr.Receive(1, bytes, len(bytes))46 if jsonReader.lastMessage.Content() != "Hello guys" {47 t.Errorf("Got message %s, want %s", jsonReader.lastMessage.Content(), "Hello guys")48 } else if jsonReader.lastMessage.Type() != 5 {49 t.Errorf("Got message type %d, want %d", jsonReader.lastMessage.Type(), 5)50 } else if jsonReader.lastMessage.ClientID() != 1 {51 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ClientID(), 1)52 } else if jsonReader.lastMessage.ID() != 10 {53 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ID(), 10)54 }55}56func TestReceive4(t *testing.T) {57 jsonReader := SimpleOutput{}58 smr := impl.SimpleTcpMessageReceiver{59 TestMode: true,60 }61 smr.SetMessageReader(&jsonReader)62 sent := "$12#1#11#12345\\$6789X"63 bytes := []byte(sent)64 smr.Receive(1, bytes, len(bytes))65 if jsonReader.lastMessage.Content() != "12345\\$6789X" {66 t.Errorf("Got message %s, want %s", jsonReader.lastMessage.Content(), "12345\\$6789X")67 } else if jsonReader.lastMessage.Type() != 1 {68 t.Errorf("Got message type %d, want %d", jsonReader.lastMessage.Type(), 5)69 } else if jsonReader.lastMessage.ClientID() != 1 {70 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ClientID(), 1)71 } else if jsonReader.lastMessage.ID() != 11 {72 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ID(), 10)73 }74}75func TestReceive5(t *testing.T) {76 jsonReader := SimpleOutput{}77 smr := impl.SimpleTcpMessageReceiver{78 TestMode: true,79 }80 smr.SetMessageReader(&jsonReader)81 sent := "$12#1#11#12345\\\\$12#1#11#12345\\\\\\$678"82 bytes := []byte(sent)83 smr.Receive(1, bytes, len(bytes))84 if jsonReader.lastMessage.Content() != "12345\\\\\\$678" {85 t.Errorf("Got message %s, want %s", jsonReader.lastMessage.Content(), "12345\\\\\\$678")86 } else if jsonReader.lastMessage.Type() != 1 {87 t.Errorf("Got message type %d, want %d", jsonReader.lastMessage.Type(), 5)88 } else if jsonReader.lastMessage.ClientID() != 1 {89 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ClientID(), 1)90 } else if jsonReader.lastMessage.ID() != 11 {91 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ID(), 10)92 }93}94func TestReceive6(t *testing.T) {95 jsonReader := SimpleOutput{}96 smr := impl.SimpleTcpMessageReceiver{97 TestMode: true,98 }99 smr.SetMessageReader(&jsonReader)100 sent := "$12#1#11#12345\\\\\\$678"101 bytes := []byte(sent)102 smr.Receive(1, bytes, len(bytes))103 if jsonReader.lastMessage.Content() != "12345\\\\\\$678" {104 t.Errorf("Got message %s, want %s", jsonReader.lastMessage.Content(), "12345\\\\\\$678")105 } else if jsonReader.lastMessage.Type() != 1 {106 t.Errorf("Got message type %d, want %d", jsonReader.lastMessage.Type(), 5)107 } else if jsonReader.lastMessage.ClientID() != 1 {108 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ClientID(), 1)109 } else if jsonReader.lastMessage.ID() != 11 {110 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ID(), 10)111 }112}113func TestReceive2(t *testing.T) {114 logrus.SetLevel(logrus.DebugLevel)115 jsonReader := SimpleOutput{}116 smr := impl.SimpleTcpMessageReceiver{117 TestMode: true,118 }119 smr.SetMessageReader(&jsonReader)120 bytec := len([]byte("{\"player_name\" : \"alzáÄek\"}"))121 sent := fmt.Sprintf("$%d#2#3#{\"player_name\" : \"alzáÄek\"}", bytec)122 bytes := []byte(sent)123 smr.Receive(1, bytes, len(bytes))124 if jsonReader.lastMessage.Content() != "{\"player_name\" : \"alzáÄek\"}" {125 t.Errorf("Got message %s, want %s", jsonReader.lastMessage.Content(), "$%d#2#3#{\"player_name\" : \"alzáÄek\"}")126 } else if jsonReader.lastMessage.Type() != 2 {127 t.Errorf("Got message type %d, want %d", jsonReader.lastMessage.Type(), 2)128 } else if jsonReader.lastMessage.ClientID() != 1 {129 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ClientID(), 1)130 } else if jsonReader.lastMessage.ID() != 3 {131 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ID(), 3)132 }133}134func TestReceivePartial(t *testing.T) {135 jsonReader := SimpleOutput{}136 smr := impl.SimpleTcpMessageReceiver{137 TestMode: true,138 }139 smr.SetMessageReader(&jsonReader)140 sent := "$10#5#10#Hello"141 bytes := []byte(sent)142 smr.Receive(1, bytes, len(bytes))143 sent = " guys"144 bytes = []byte(sent)145 smr.Receive(1, bytes, len(bytes))146 if jsonReader.lastMessage.Content() != "Hello guys" {147 t.Errorf("Got message %s, want %s", jsonReader.lastMessage.Content(), "Hello guys")148 } else if jsonReader.lastMessage.Type() != 5 {149 t.Errorf("Got message type %d, want %d", jsonReader.lastMessage.Type(), 5)150 } else if jsonReader.lastMessage.ClientID() != 1 {151 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ClientID(), 1)152 } else if jsonReader.lastMessage.ID() != 10 {153 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ID(), 10)154 }155}156func TestReceivePartial2(t *testing.T) {157 jsonReader := SimpleOutput{}158 smr := impl.SimpleTcpMessageReceiver{159 TestMode: true,160 }161 smr.SetMessageReader(&jsonReader)162 sent := "$10#5#10#Youre"163 bytes := []byte(sent)164 smr.Receive(1, bytes, len(bytes))165 sent = " guys hello"166 bytes = []byte(sent)167 smr.Receive(1, bytes, len(bytes))168 sent = "$10#5#10#Hello guys"169 bytes = []byte(sent)170 smr.Receive(1, bytes, len(bytes))171 if jsonReader.lastMessage.Content() != "Hello guys" {172 t.Errorf("Got message %s, want %s", jsonReader.lastMessage.Content(), "Hello guys")173 } else if jsonReader.lastMessage.Type() != 5 {174 t.Errorf("Got message type %d, want %d", jsonReader.lastMessage.Type(), 5)175 } else if jsonReader.lastMessage.ClientID() != 1 {176 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ClientID(), 1)177 } else if jsonReader.lastMessage.ID() != 10 {178 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ID(), 10)179 }180}181func TestReceivePartial3(t *testing.T) {182 jsonReader := SimpleOutput{}183 smr := impl.SimpleTcpMessageReceiver{184 TestMode: true,185 }186 smr.SetMessageReader(&jsonReader)187 sent := "$10#5#10#Hello\\"188 bytes := []byte(sent)189 smr.Receive(1, bytes, len(bytes))190 sent = "$guy"191 bytes = []byte(sent)192 smr.Receive(1, bytes, len(bytes))193 if jsonReader.lastMessage.Content() != "Hello\\$guy" {194 t.Errorf("Got message %s, want %s", jsonReader.lastMessage.Content(), "Hello$guy")195 } else if jsonReader.lastMessage.Type() != 5 {196 t.Errorf("Got message type %d, want %d", jsonReader.lastMessage.Type(), 5)197 } else if jsonReader.lastMessage.ClientID() != 1 {198 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ClientID(), 1)199 } else if jsonReader.lastMessage.ID() != 10 {200 t.Errorf("Got client UID %d, want %d", jsonReader.lastMessage.ID(), 10)201 }202}203func TestReceiveMultiple(t *testing.T) {204 jsonReader := SimpleOutput{}205 smr := impl.SimpleTcpMessageReceiver{206 TestMode: true,207 }208 smr.SetMessageReader(&jsonReader)209 bytec := len([]byte("PÄšák"))210 sent := fmt.Sprintf("$10#5#10#Hello\\$guy$%d#7#11#PÄšák", bytec)211 bytes := []byte(sent)212 smr.Receive(1, bytes, len(bytes))213 if jsonReader.messages[0].Content() != "Hello\\$guy" {214 t.Errorf("Got message %s, want %s", jsonReader.messages[0].Content(), "Hello\\$guy")215 } else if jsonReader.messages[0].Type() != 5 {216 t.Errorf("Got message type %d, want %d", jsonReader.messages[0].Type(), 5)217 } else if jsonReader.messages[0].ClientID() != 1 {218 t.Errorf("Got client UID %d, want %d", jsonReader.messages[0].ClientID(), 1)219 } else if jsonReader.messages[0].ID() != 10 {220 t.Errorf("Got client UID %d, want %d", jsonReader.messages[0].ID(), 10)221 }222 if jsonReader.messages[1].Content() != "PÄšák" {223 t.Errorf("Got message %s, want %s", jsonReader.messages[1].Content(), "PÄšák")224 } else if jsonReader.messages[1].Type() != 7 {225 t.Errorf("Got message type %d, want %d", jsonReader.messages[1].Type(), 7)226 } else if jsonReader.messages[1].ClientID() != 1 {227 t.Errorf("Got client UID %d, want %d", jsonReader.messages[1].ClientID(), 1)228 } else if jsonReader.messages[1].ID() != 11 {229 t.Errorf("Got client UID %d, want %d", jsonReader.messages[1].ID(), 10)230 }231}232func TestReceiveMultiple2(t *testing.T) {233 jsonReader := SimpleOutput{}234 smr := impl.SimpleTcpMessageReceiver{235 TestMode: true,236 }237 smr.SetMessageReader(&jsonReader)238 bytec := len([]byte("PÄšáÄeÄÃkk"))239 sent := fmt.Sprintf("$10#5#10#Hello\\$guy$%d#7#11#PÄšáÄeÄÃkk", bytec)240 bytes := []byte(sent)241 smr.Receive(1, bytes, len(bytes))242 if jsonReader.messages[0].Content() != "Hello\\$guy" {243 t.Errorf("Got message %s, want %s", jsonReader.messages[0].Content(), "Hello\\$guy")244 } else if jsonReader.messages[0].Type() != 5 {245 t.Errorf("Got message type %d, want %d", jsonReader.messages[0].Type(), 5)246 } else if jsonReader.messages[0].ClientID() != 1 {247 t.Errorf("Got client UID %d, want %d", jsonReader.messages[0].ClientID(), 1)248 } else if jsonReader.messages[0].ID() != 10 {249 t.Errorf("Got client UID %d, want %d", jsonReader.messages[0].ID(), 10)250 }251 if jsonReader.messages[1].Content() != "PÄšáÄeÄÃkk" {252 t.Errorf("Got message %s, want %s", jsonReader.messages[1].Content(), "PÄšák")253 } else if jsonReader.messages[1].Type() != 7 {254 t.Errorf("Got message type %d, want %d", jsonReader.messages[1].Type(), 7)255 } else if jsonReader.messages[1].ClientID() != 1 {256 t.Errorf("Got client UID %d, want %d", jsonReader.messages[1].ClientID(), 1)257 } else if jsonReader.messages[1].ID() != 11 {258 t.Errorf("Got client UID %d, want %d", jsonReader.messages[1].ID(), 10)259 }260}261func TestReceiveMultipleSplit(t *testing.T) {262 jsonReader := SimpleOutput{}263 smr := impl.SimpleTcpMessageReceiver{264 TestMode: true,265 }266 smr.SetMessageReader(&jsonReader)267 bytec := len([]byte("PÄšák"))268 sent := fmt.Sprintf("$10#5#10#Hello\\$guy$%d#7#11#PÄÅ¡", bytec)269 bytes := []byte(sent)270 smr.Receive(1, bytes, len(bytes))271 sent = "ák"272 bytes = []byte(sent)273 smr.Receive(1, bytes, len(bytes))274 if jsonReader.messages[0].Content() != "Hello\\$guy" {275 t.Errorf("Got message %s, want %s", jsonReader.messages[0].Content(), "Hello\\$guy")276 } else if jsonReader.messages[0].Type() != 5 {277 t.Errorf("Got message type %d, want %d", jsonReader.messages[0].Type(), 5)278 } else if jsonReader.messages[0].ClientID() != 1 {279 t.Errorf("Got client UID %d, want %d", jsonReader.messages[0].ClientID(), 1)280 } else if jsonReader.messages[0].ID() != 10 {281 t.Errorf("Got client UID %d, want %d", jsonReader.messages[0].ID(), 10)282 }283 if jsonReader.messages[1].Content() != "PÄšák" {284 t.Errorf("Got message %s, want %s", jsonReader.messages[1].Content(), "PÄšák")285 } else if jsonReader.messages[1].Type() != 7 {286 t.Errorf("Got message type %d, want %d", jsonReader.messages[1].Type(), 7)287 } else if jsonReader.messages[1].ClientID() != 1 {288 t.Errorf("Got client UID %d, want %d", jsonReader.messages[1].ClientID(), 1)289 } else if jsonReader.messages[1].ID() != 11 {290 t.Errorf("Got client UID %d, want %d", jsonReader.messages[1].ID(), 10)291 }292}...
log_test.go
Source:log_test.go
...11 "github.com/stretchr/testify/assert"12)13type MockWriter struct {14 LastLevel level.Level15 LastMessage string16 WriteCount int17 ReOpenCount int18 CloseCount int19}20func (w *MockWriter) Write(le level.Level, module string, message string) error {21 w.LastLevel = le22 w.LastMessage = message23 w.WriteCount++24 return nil25}26func (w *MockWriter) ReOpen() error {27 w.ReOpenCount++28 return nil29}30func (w *MockWriter) Close() error {31 w.CloseCount++32 return nil33}34func TestGlobalFilters(t *testing.T) {35 log.Reset()36 mockWriter := &MockWriter{}37 log.DefaultLogWriter(mockWriter)38 log.AddGlobalFilter(func(filter filter.Filter) filter.Filter {39 return func(f event.Event) {40 f.Fields.CustomFields["user_id"] = 12341 filter(f)42 }43 })44 log.Emergency("Hello")45 assert.Equal(t, level.Emergency, mockWriter.LastLevel)46 assert.Regexp(t, regexp.MustCompile("^\\[.*?\\] .*?[DEBUG].*?Hello.*?\"user_id\":123"), mockWriter.LastMessage)47}48func TestFilters(t *testing.T) {49 log.Reset()50 mockWriter := &MockWriter{}51 log.DefaultLogWriter(mockWriter)52 log.DefaultLogFormatter(formatter.NewDefaultFormatter(false))53 log.Module("test").AddFilter(func(filter filter.Filter) filter.Filter {54 return func(f event.Event) {55 f.Fields.CustomFields["user_id"] = 12356 filter(f)57 }58 })59 log.Module("test").Debug("hello")60 assert.Regexp(t, regexp.MustCompile("\"user_id\":123"), mockWriter.LastMessage)61 log.Module("test2").Debug("hello")62 assert.NotRegexp(t, regexp.MustCompile("\"user_id\":123"), mockWriter.LastMessage)63}64func TestGlobalFields(t *testing.T) {65 log.Reset()66 mockWriter := &MockWriter{}67 log.DefaultLogFormatter(formatter.NewDefaultFormatter(false))68 log.DefaultLogWriter(mockWriter)69 log.GlobalFields(func(c event.Fields) {70 c.GlobalFields["ref"] = "abcd"71 c.CustomFields["user_id"] = 12372 })73 log.Module("test").Debug("hello")74 assert.Regexp(t, regexp.MustCompile("#ref"), mockWriter.LastMessage)75 assert.Regexp(t, regexp.MustCompile("\"user_id\""), mockWriter.LastMessage)76 log.Module("test").GlobalFields(func(c event.Fields) {77 c.CustomFields["enterprise_id"] = 12378 })79 log.Module("test").Debug("hello")80 assert.NotRegexp(t, regexp.MustCompile("#ref"), mockWriter.LastMessage)81 assert.NotRegexp(t, regexp.MustCompile("\"user_id\""), mockWriter.LastMessage)82 assert.Regexp(t, regexp.MustCompile("\"enterprise_id\""), mockWriter.LastMessage)83 log.Module("test").GlobalFields(func(c event.Fields) {84 c.CustomFields["enterprise_id"] = 12385 log.GetDefaultConfig().GlobalFields(c)86 })87 log.Module("test").Debug("hello")88 assert.Regexp(t, regexp.MustCompile("#ref"), mockWriter.LastMessage)89 assert.Regexp(t, regexp.MustCompile("\"user_id\""), mockWriter.LastMessage)90 assert.Regexp(t, regexp.MustCompile("\"enterprise_id\""), mockWriter.LastMessage)91}92func TestBasicLog(t *testing.T) {93 log.Reset()94 mockWriter := &MockWriter{}95 log.DefaultLogFormatter(formatter.NewDefaultFormatter(false))96 log.DefaultLogWriter(mockWriter)97 log.Emergency("hello")98 assert.Equal(t, level.Emergency, mockWriter.LastLevel)99 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {.*?}", level.Emergency.GetLevelName())), mockWriter.LastMessage)100 log.Alert("hello")101 assert.Equal(t, level.Alert, mockWriter.LastLevel)102 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {.*?}", level.Alert.GetLevelName())), mockWriter.LastMessage)103 log.Critical("hello")104 assert.Equal(t, level.Critical, mockWriter.LastLevel)105 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {.*?}", level.Critical.GetLevelName())), mockWriter.LastMessage)106 log.Error("hello")107 assert.Equal(t, level.Error, mockWriter.LastLevel)108 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {.*?}", level.Error.GetLevelName())), mockWriter.LastMessage)109 log.Warning("hello")110 assert.Equal(t, level.Warning, mockWriter.LastLevel)111 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {}", level.Warning.GetLevelName())), mockWriter.LastMessage)112 log.Notice("hello")113 assert.Equal(t, level.Notice, mockWriter.LastLevel)114 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {}", level.Notice.GetLevelName())), mockWriter.LastMessage)115 log.Info("hello")116 assert.Equal(t, level.Info, mockWriter.LastLevel)117 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {}", level.Info.GetLevelName())), mockWriter.LastMessage)118 log.Debug("hello")119 assert.Equal(t, level.Debug, mockWriter.LastLevel)120 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello {.*?}", level.Debug.GetLevelName())), mockWriter.LastMessage)121}122func TestBasicLogf(t *testing.T) {123 log.Reset()124 mockWriter := &MockWriter{}125 log.DefaultLogFormatter(formatter.NewDefaultFormatter(false))126 log.DefaultLogWriter(mockWriter)127 log.Emergencyf("hello %s", "world")128 assert.Equal(t, level.Emergency, mockWriter.LastLevel)129 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {.*?}", level.Emergency.GetLevelName())), mockWriter.LastMessage)130 log.Alertf("hello %s", "world")131 assert.Equal(t, level.Alert, mockWriter.LastLevel)132 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {.*?}", level.Alert.GetLevelName())), mockWriter.LastMessage)133 log.Criticalf("hello %s", "world")134 assert.Equal(t, level.Critical, mockWriter.LastLevel)135 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {.*?}", level.Critical.GetLevelName())), mockWriter.LastMessage)136 log.Errorf("hello %s", "world")137 assert.Equal(t, level.Error, mockWriter.LastLevel)138 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {.*?}", level.Error.GetLevelName())), mockWriter.LastMessage)139 log.Warningf("hello %s", "world")140 assert.Equal(t, level.Warning, mockWriter.LastLevel)141 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {}", level.Warning.GetLevelName())), mockWriter.LastMessage)142 log.Noticef("hello %s", "world")143 assert.Equal(t, level.Notice, mockWriter.LastLevel)144 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {}", level.Notice.GetLevelName())), mockWriter.LastMessage)145 log.Infof("hello %s", "world")146 assert.Equal(t, level.Info, mockWriter.LastLevel)147 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {}", level.Info.GetLevelName())), mockWriter.LastMessage)148 log.Debugf("hello %s", "world")149 assert.Equal(t, level.Debug, mockWriter.LastLevel)150 assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("^\\[.*?\\] %s .*? hello world {.*?}", level.Debug.GetLevelName())), mockWriter.LastMessage)151}152func TestLogger_ReOpenClose(t *testing.T) {153 log.Reset()154 mockWriter := &MockWriter{}155 log.SetWriter(mockWriter)156 assert.NoError(t, log.Close())157 assert.Equal(t, 1, mockWriter.CloseCount)158 assert.NoError(t, log.ReOpen())159 assert.Equal(t, 1, mockWriter.ReOpenCount)160 assert.Equal(t, 1, mockWriter.CloseCount)161 for _, err := range log.CloseAll() {162 assert.NoError(t, err)163 }164 assert.Equal(t, 2, mockWriter.CloseCount)165 assert.Equal(t, 1, mockWriter.ReOpenCount)166 for _, err := range log.ReOpenAll() {167 assert.NoError(t, err)168 }169 assert.Equal(t, 2, mockWriter.CloseCount)170 assert.Equal(t, 2, mockWriter.ReOpenCount)171}172func TestLogger_LogLevel(t *testing.T) {173 log.Reset()174 mockWriter := &MockWriter{}175 log.SetWriter(mockWriter)176 log.SetLevel(level.Info)177 log.Debug("hello")178 assert.Equal(t, level.Level(0), mockWriter.LastLevel)179 assert.Equal(t, "", mockWriter.LastMessage)180 log.Warning("hello")181 assert.Equal(t, level.Warning, mockWriter.LastLevel)182 assert.NotEqual(t, "", mockWriter.LastMessage)183 log.Info("info message")184 assert.Equal(t, level.Info, mockWriter.LastLevel)185 assert.Regexp(t, regexp.MustCompile("info message"), mockWriter.LastMessage)186 log.Debug("debug message")187 assert.Equal(t, level.Info, mockWriter.LastLevel)188 assert.Regexp(t, regexp.MustCompile("info message"), mockWriter.LastMessage)189}190func TestLogger_Formatter(t *testing.T) {191 log.Reset()192 mockWriter := &MockWriter{}193 log.SetWriter(mockWriter)194 log.SetFormatter(formatter.NewJSONFormatter())195 log.Debug("hello")196 assert.Equal(t, level.Debug, mockWriter.LastLevel)197 assert.Regexp(t, regexp.MustCompile("^{.*}$"), mockWriter.LastMessage)198 log.Warning("warning")199 assert.Equal(t, level.Warning, mockWriter.LastLevel)200 assert.Regexp(t, regexp.MustCompile("^{.*}$"), mockWriter.LastMessage)201}202func TestLoggers_WithFields(t *testing.T) {203 log.Reset()204 mockWriter := &MockWriter{}205 log.SetWriter(mockWriter)206 log.SetFormatter(formatter.NewJSONFormatter())207 log.With(123).Debugf("hello, world")208 assert.Regexp(t, regexp.MustCompile("\"@\":123"), mockWriter.LastMessage)209}...
LastMessage
Using AI Code Generation
1import (2func main() {3 fmt.Println(test.LastMessage())4}5import (6func main() {7 fmt.Println(test.LastMessage())8}9import (10func main() {11 fmt.Println(test.LastMessage())12}13import (14func main() {15 fmt.Println(test.LastMessage())16}17import (18func main() {19 fmt.Println(test.LastMessage())20}21import (22func main() {23 fmt.Println(test.LastMessage())24}25import (26func main() {27 fmt.Println(test.LastMessage())28}29import (30func main() {31 fmt.Println(test.LastMessage())32}33import (34func main() {35 fmt.Println(test.LastMessage())36}37import (38func main() {39 fmt.Println(test.LastMessage())40}41import (42func main() {43 fmt.Println(test.LastMessage())44}45import (46func main() {47 fmt.Println(test.LastMessage())48}
LastMessage
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("Hello, world.")4}5import "fmt"6import "test"7func main() {8 fmt.Println("Hello, world.")9 fmt.Println(test.LastMessage())10}11The go get command will automatically download the package and put it in the right place for you. You can then import it with import "github.com/username/test"
LastMessage
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 test := Test{}5 test.LastMessage()6}7import (8type Test struct {9}10func (t *Test) LastMessage() {11 fmt.Println("Hello, playground")12}13import (14func main() {15 fmt.Println(myVar)16 fmt.Println(myVar)17}18./main.go:12: cannot use 5 (type untyped int) as type string in assignment19import (20func main() {21 fmt.Println(myVar)22 fmt.Println(myVar)23}24./main.go:12: cannot use 5 (type untyped int) as type string in assignment
LastMessage
Using AI Code Generation
1import (2func main() {3 fmt.Println(test.LastMessage())4}5If the package name is different from the folder name, then we can use the following command to import the package:6import "folder_name/package_name"7import (8func main() {9 fmt.Println(test.LastMessage())10}11import "folder_name/package_name"12import (13func main() {14 fmt.Println(test.LastMessage())15}16import "folder_name/package_name"17import (18func main() {19 fmt.Println(test.LastMessage())20}21import "folder_name/package_name"22import (23func main() {24 fmt.Println(test.LastMessage())25}26import "folder_name/package_name"27import (
LastMessage
Using AI Code Generation
1import (2func main() {3 t := test.New()4 fmt.Println(t.LastMessage())5}6import (7func main() {8 t := test.New()9 fmt.Println(t.LastMessage())10}11import (12func main() {13 t := test.New()14 fmt.Println(t.LastMessage())15}16import (17func main() {18 t := test.New()19 fmt.Println(t.LastMessage())20}21import (22func main() {23 t := test.New()24 fmt.Println(t.LastMessage())25}26import (27func main() {28 t := test.New()29 fmt.Println(t.LastMessage())30}31import (32func main() {33 t := test.New()34 fmt.Println(t.LastMessage())35}36import (37func main() {38 t := test.New()39 fmt.Println(t.LastMessage())40}41import (42func main() {
LastMessage
Using AI Code Generation
1import (2func main() {3 fmt.Println(test.LastMessage())4}5import "fmt"6import (7import (8import (9import (10import (11import (12func function_name( [parameter list] ) [return_types]13{14}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!