Пример #1
0
func (s *State) waitForNextCommand(dev Device, ch chan bool) {
	for {
		pfd := []unix.PollFd{
			{
				Fd:      devFd,
				Events:  unix.POLLIN,
				Revents: 0,
			},
			{
				Fd:      int32(pipeFds[0]),
				Events:  unix.POLLIN,
				Revents: 0,
			},
		}
		_, err := unix.Poll(pfd, -1)
		if err != nil {
			log.Errorln("Poll command failed: ", err)
			ch <- false
			break
		}
		if pfd[1].Revents == unix.POLLIN {
			log.Infoln("Poll command receive finish signal")
			ch <- false
			break
		}
		if pfd[0].Revents != 0 && pfd[0].Revents != unix.POLLIN {
			log.Errorln("Poll received unexpect event: ", pfd[0].Revents)
			ch <- false
			break
		}
		ch <- true
	}
}
Пример #2
0
func TestPoll(t *testing.T) {
	f, cleanup := mktmpfifo(t)
	defer cleanup()

	const timeout = 100

	ok := make(chan bool, 1)
	go func() {
		select {
		case <-time.After(10 * timeout * time.Millisecond):
			t.Errorf("Poll: failed to timeout after %d milliseconds", 10*timeout)
		case <-ok:
		}
	}()

	fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
	n, err := unix.Poll(fds, timeout)
	ok <- true
	if err != nil {
		t.Errorf("Poll: unexpected error: %v", err)
		return
	}
	if n != 0 {
		t.Errorf("Poll: wrong number of events: got %v, expected %v", n, 0)
		return
	}
}