Ejemplo n.º 1
0
func (s *FlushContextSuite) TestRunHookMetricSendingFailedByServer(c *gc.C) {
	uuid, err := utils.NewUUID()
	c.Assert(err, jc.ErrorIsNil)
	ctx := s.getMeteredHookContext(c, uuid.String(), -1, "", noProxies, true, s.metricsDefinition("pings"))

	// Send batches once.
	batches := []runner.MetricsBatch{
		{
			CharmURL: s.meteredCharm.URL().String(),
			UUID:     utils.MustNewUUID().String(),
			Created:  time.Now(),
			Metrics:  []jujuc.Metric{{Key: "pings", Value: "1", Time: time.Now()}},
		}, {
			CharmURL: s.meteredCharm.URL().String(),
			UUID:     utils.MustNewUUID().String(),
			Created:  time.Now(),
			Metrics:  []jujuc.Metric{{Key: "pings", Value: "1", Time: time.Now()}},
		},
	}

	reader := &StubMetricsReader{
		Stub:    &s.Stub,
		Batches: batches,
	}

	restoreRunner := runner.PatchMetricsReader(ctx, reader)
	defer restoreRunner()

	restoreSender := runner.PatchMetricsSender(ctx, func(batches []params.MetricBatch) (map[string]error, error) {
		responses := make(map[string]error, len(batches))
		for i := range responses {
			responses[i] = errors.New("failed to store")
		}
		return responses, nil
	})
	defer restoreSender()

	// Flush the context.
	err = ctx.FlushContext("some badge", nil)
	c.Assert(err, jc.ErrorIsNil)

	// Check stub calls, metrics should not be removed.
	s.Stub.CheckCallNames(c, "Open", "Close")
	s.Stub.ResetCalls()
}
Ejemplo n.º 2
0
func (s *FlushContextSuite) TestRunHookMetricSendingGetDuplicate(c *gc.C) {
	uuid := utils.MustNewUUID()
	ctx := s.getMeteredHookContext(c, uuid.String(), -1, "", noProxies, true, s.metricsDefinition("pings"))

	// Send batches once.
	batches := []runner.MetricsBatch{
		{
			CharmURL: s.meteredCharm.URL().String(),
			UUID:     utils.MustNewUUID().String(),
			Created:  time.Now(),
			Metrics:  []jujuc.Metric{{Key: "pings", Value: "1", Time: time.Now()}},
		}, {
			CharmURL: s.meteredCharm.URL().String(),
			UUID:     utils.MustNewUUID().String(),
			Created:  time.Now(),
			Metrics:  []jujuc.Metric{{Key: "pings", Value: "1", Time: time.Now()}},
		},
	}

	reader := &StubMetricsReader{
		Stub:    &s.Stub,
		Batches: batches,
	}

	runner.PatchMetricsReader(ctx, reader)

	// Flush the context with a success.
	err := ctx.FlushContext("some badge", nil)
	c.Assert(err, jc.ErrorIsNil)

	// Check stub calls.
	s.Stub.CheckCallNames(c, "Open", "Remove", "Remove", "Close")
	s.Stub.ResetCalls()
	metricBatches, err := s.State.MetricBatches()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(metricBatches, gc.HasLen, 2)

	// Create a new context with a duplicate metrics batch.
	uuid = utils.MustNewUUID()
	ctx = s.getMeteredHookContext(c, uuid.String(), -1, "", noProxies, true, s.metricsDefinition("pings"))
	runner.PatchMetricsReader(ctx, reader)

	newBatches := []runner.MetricsBatch{
		batches[0],
		{
			CharmURL: s.meteredCharm.URL().String(),
			UUID:     utils.MustNewUUID().String(),
			Created:  time.Now(),
			Metrics:  []jujuc.Metric{{Key: "pings", Value: "1", Time: time.Now()}},
		},
	}
	reader.Batches = newBatches

	// Flush the context with a success.
	err = ctx.FlushContext("some badge", nil)
	c.Assert(err, jc.ErrorIsNil)

	// Check stub calls.
	s.Stub.CheckCallNames(c, "Open", "Remove", "Remove", "Close")

	metricBatches, err = s.State.MetricBatches()
	c.Assert(err, jc.ErrorIsNil)
	// Only one additional metric has been recorded.
	c.Assert(metricBatches, gc.HasLen, 3)

}