Beispiel #1
0
func (s *pingerSuite) calculatePingTimeout(c *gc.C) time.Duration {
	// Try opening an API connection a few times and take the max
	// delay among the attempts.
	attempt := utils.AttemptStrategy{
		Delay: coretesting.ShortWait,
		Min:   3,
	}
	var maxTimeout time.Duration
	for a := attempt.Start(); a.Next(); {
		openStart := time.Now()
		st, _ := s.OpenAPIAsNewMachine(c)
		err := st.Ping()
		if c.Check(err, jc.ErrorIsNil) {
			openDelay := time.Since(openStart)
			c.Logf("API open and initial ping took %v", openDelay)
			if maxTimeout < openDelay {
				maxTimeout = openDelay
			}
		}
		if st != nil {
			c.Check(st.Close(), jc.ErrorIsNil)
		}
	}
	if !c.Failed() && maxTimeout > 0 {
		return maxTimeout
	}
	c.Fatalf("cannot calculate ping timeout")
	return 0
}
Beispiel #2
0
func doConcurrentTest(c *test.C, ct func()) {
	maxProcs, numReqs := 1, 150
	if testing.Short() {
		maxProcs, numReqs = 4, 50
	}
	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(maxProcs))

	var wg sync.WaitGroup
	wg.Add(numReqs)

	reqs := make(chan bool)
	defer close(reqs)

	for i := 0; i < maxProcs*2; i++ {
		go func() {
			for _ = range reqs {
				ct()
				if c.Failed() {
					wg.Done()
					continue
				}
				wg.Done()
			}
		}()
	}

	for i := 0; i < numReqs; i++ {
		reqs <- true
	}

	wg.Wait()
}
Beispiel #3
0
func (s *Suite) CheckFailed(c *gc.C) {
	if c.Failed() {
		c.Succeed()
	} else {
		c.Errorf("expected failure; none observed")
	}
	c.Logf("-------------------------------")
}
Beispiel #4
0
func verifyTransitions(model Stream, outcomes transitionCases, c *gc.C) {
	var pump chan int
	var underTest *Stream

	from := func() *Stream {
		pump = make(chan int, 1)
		underTest = &Stream{
			ID:                model.ID,
			State:             model.State,
			SendFlowAvailable: 4096,
			SendFlowPump:      pump,
		}
		return underTest
	}

	verify := func(outcome interface{}, err *Error) {
		if expected, ok := outcome.(*errCase); ok {
			c.Check(err, gc.NotNil)
			c.Check(err.Level, gc.Equals, expected.level)
			c.Check(err.Code, gc.Equals, expected.code)
			return
		}
		expected := outcome.(*successCase)
		c.Check(underTest.State, gc.Equals, expected.state)

		select {
		case r, ok := <-pump:
			if expected.pumpOpened {
				c.Check(ok, gc.Equals, true)
				c.Check(r, gc.Equals, underTest.SendFlowAvailable)
			} else if expected.pumpClosed {
				c.Check(ok, gc.Equals, false)
			} else {
				c.Error("unexpected pump update: ", r, ok)
			}
		default:
			c.Check(expected.pumpOpened, gc.Equals, false)
			c.Check(expected.pumpClosed, gc.Equals, false)
		}
		if c.Failed() {
			panic(false) // Generate a callstack.
		}
	}

	verify(outcomes.onRecvData, from().onData(Receive, false))
	verify(outcomes.onRecvDataWithFin, from().onData(Receive, true))
	verify(outcomes.onRecvHeaders, from().onHeaders(Receive, false))
	verify(outcomes.onRecvHeadersWithFin, from().onHeaders(Receive, true))
	verify(outcomes.onRecvPushPromise, from().onPushPromise(Receive))
	verify(outcomes.onRecvReset, from().onReset(Receive))
	verify(outcomes.onSendData, from().onData(Send, false))
	verify(outcomes.onSendDataWithFin, from().onData(Send, true))
	verify(outcomes.onSendHeaders, from().onHeaders(Send, false))
	verify(outcomes.onSendHeadersWithFin, from().onHeaders(Send, true))
	verify(outcomes.onSendPushPromise, from().onPushPromise(Send))
	verify(outcomes.onSendReset, from().onReset(Send))
}
Beispiel #5
0
func (s *LegacySuite) checkTarContents(
	c *gc.C, tarFile io.Reader, allExpected []tarContent,
) {
	contents := readTarFile(c, tarFile)

	// Check that the expected entries are there.
	// XXX Check for unexpected entries.
	for _, expected := range allExpected {
		relPath := strings.TrimPrefix(expected.Name, string(os.PathSeparator))

		c.Log(fmt.Sprintf("checking for presence of %q on tar file", relPath))
		body, found := contents[relPath]
		if !found {
			c.Errorf("%q not found", expected.Name)
			continue
		}

		if expected.Body != "" {
			c.Log("Also checking the file contents")
			c.Check(body, gc.Equals, expected.Body)
		}

		if expected.Nested != nil {
			c.Log("Also checking the nested tar file")
			nestedFile := bytes.NewBufferString(body)
			s.checkTarContents(c, nestedFile, expected.Nested)
		}
	}

	if c.Failed() {
		c.Log("-----------------------")
		c.Log("expected:")
		for _, expected := range allExpected {
			c.Log(fmt.Sprintf("%s -> %q", expected.Name, expected.Body))
		}
		c.Log("got:")
		for name, body := range contents {
			if len(body) > 200 {
				body = body[:200] + "...(truncated)"
			}
			c.Log(fmt.Sprintf("%s -> %q", name, body))
		}
	}
}
Beispiel #6
0
func (s *FastPeriodSuite) TestWatchCollection(c *gc.C) {
	chA1 := make(chan watcher.Change)
	chB1 := make(chan watcher.Change)
	chA := make(chan watcher.Change)
	chB := make(chan watcher.Change)

	s.w.Watch("testA", 1, -1, chA1)
	s.w.Watch("testB", 1, -1, chB1)
	s.w.WatchCollection("testA", chA)
	s.w.WatchCollection("testB", chB)

	revno1 := s.insert(c, "testA", 1)
	revno2 := s.insert(c, "testA", 2)
	revno3 := s.insert(c, "testB", 1)
	revno4 := s.insert(c, "testB", 2)

	s.w.StartSync()

	seen := map[chan<- watcher.Change][]watcher.Change{}
	timeout := time.After(testing.LongWait)
	n := 0
Loop1:
	for n < 6 {
		select {
		case chg := <-chA1:
			seen[chA1] = append(seen[chA1], chg)
		case chg := <-chB1:
			seen[chB1] = append(seen[chB1], chg)
		case chg := <-chA:
			seen[chA] = append(seen[chA], chg)
		case chg := <-chB:
			seen[chB] = append(seen[chB], chg)
		case <-timeout:
			break Loop1
		}
		n++
	}

	c.Check(seen[chA1], gc.DeepEquals, []watcher.Change{{"testA", 1, revno1}})
	c.Check(seen[chB1], gc.DeepEquals, []watcher.Change{{"testB", 1, revno3}})
	c.Check(seen[chA], gc.DeepEquals, []watcher.Change{{"testA", 1, revno1}, {"testA", 2, revno2}})
	c.Check(seen[chB], gc.DeepEquals, []watcher.Change{{"testB", 1, revno3}, {"testB", 2, revno4}})
	if c.Failed() {
		return
	}

	s.w.UnwatchCollection("testB", chB)
	s.w.Unwatch("testB", 1, chB1)

	revno1 = s.update(c, "testA", 1)

	s.w.StartSync()

	timeout = time.After(testing.LongWait)
	seen = map[chan<- watcher.Change][]watcher.Change{}
	n = 0
Loop2:
	for n < 2 {
		select {
		case chg := <-chA1:
			seen[chA1] = append(seen[chA1], chg)
		case chg := <-chB1:
			seen[chB1] = append(seen[chB1], chg)
		case chg := <-chA:
			seen[chA] = append(seen[chA], chg)
		case chg := <-chB:
			seen[chB] = append(seen[chB], chg)
		case <-timeout:
			break Loop2
		}
		n++
	}
	c.Check(seen[chA1], gc.DeepEquals, []watcher.Change{{"testA", 1, revno1}})
	c.Check(seen[chB1], gc.IsNil)
	c.Check(seen[chA], gc.DeepEquals, []watcher.Change{{"testA", 1, revno1}})
	c.Check(seen[chB], gc.IsNil)

	// Check that no extra events arrive.
	seen = map[chan<- watcher.Change][]watcher.Change{}
	timeout = time.After(testing.ShortWait)
Loop3:
	for {
		select {
		case chg := <-chA1:
			seen[chA1] = append(seen[chA1], chg)
		case chg := <-chB1:
			seen[chB1] = append(seen[chB1], chg)
		case chg := <-chA:
			seen[chA] = append(seen[chA], chg)
		case chg := <-chB:
			seen[chB] = append(seen[chB], chg)
		case <-timeout:
			break Loop3
		}
	}
	c.Check(seen[chA1], gc.IsNil)
	c.Check(seen[chB1], gc.IsNil)
	c.Check(seen[chA], gc.IsNil)
	c.Check(seen[chB], gc.IsNil)
}
func (suite *DirectoryReleaseSuite) TearDownSuite(c *check.C) {
	if !c.Failed() {
		os.RemoveAll(suite.dynPath)
	}
}
func (suite *FileResourceSuite) TearDownSuite(c *check.C) {
	if !c.Failed() {
		os.RemoveAll(suite.dynPath)
	}
}