示例#1
0
func SendInstruction(br *s.Bracelet, from, to Expect, command string, header string, check bool, t *testing.T) (*s.Bracelet, Expect, Expect) {
	if command != "" {
		if _, err := br.Write(command); err != nil {
			llog.Error("Err", "error", err)
			t.Fatal("Can't write message to bracelet")
		}
		br.Logger.Info("Execute", "command", command)
		to.Check(command, "to bracelet")
	}
	ch := make(chan struct{}, 200)
	for i := 0; i < 200; i++ {
		ch <- struct{}{}
	}
	go Listen(br, from, to, t, ch)
	for {
		if header == "" {
			time.Sleep(time.Second * 5)
			break
		}
		if from[header].Msg == "" {
			time.Sleep(time.Second * 5)
			continue
		}
		if !from.CheckStatus(header) && check {
			t.Fatal("Wrong code")
		}
		break
	}
	Wait()
	close(ch)
	return br, from, to
}
示例#2
0
func Listen(br *s.Bracelet, expectedRequests, expectedResponses Expect, t *testing.T, ch <-chan struct{}) {
	for range ch {
		request, err := br.Read()
		if err != nil {
			select {
			case _, ok := <-ch:
				if !ok {
					return
				}
			default:
				t.Fatal("Can't read message from bracelet")
			}
		}
		strRequest := string(request)
		expectedRequests.Check(strRequest, "from bracelet")
		response, err := server.AnswerRequest(br.ID, strRequest)
		switch err {
		case s.NoResponse:
			break
		case nil:
			if _, err = br.Write(response); err != nil {
				br.Conn.Close()
				select {
				case _, ok := <-ch:
					if !ok {
						return
					}

				default:
					t.Fatal("Can't write message to braclet")
				}
			}
			expectedResponses.Check(response, "to bracelet")
			break
		default:
			t.Fatal(fmt.Sprintf("Can't find answer for %s", strRequest))
		}
	}
}