func newTestAMQPCanceller(t *testing.T) *AMQPCanceller {
	amqpConn, _ := setupConn(t)

	uuid := uuid.NewRandom()
	ctx := context.FromUUID(gocontext.TODO(), uuid.String())

	return NewAMQPCanceller(ctx, amqpConn)
}
func TestAMQPLogWriterWrite(t *testing.T) {
	amqpConn, amqpChan := setupConn(t)
	defer amqpConn.Close()
	defer amqpChan.Close()

	uuid := uuid.NewRandom()
	ctx := workerctx.FromUUID(context.TODO(), uuid.String())

	logWriter, err := newAMQPLogWriter(ctx, amqpConn, 4)
	if err != nil {
		t.Fatal(err)
	}
	logWriter.SetMaxLogLength(1000)
	logWriter.SetTimeout(time.Second)

	_, err = fmt.Fprintf(logWriter, "Hello, ")
	if err != nil {
		t.Error(err)
	}
	_, err = fmt.Fprintf(logWriter, "world!")
	if err != nil {
		t.Error(err)
	}

	// Close the log writer to force it to flush out the buffer
	err = logWriter.Close()
	if err != nil {
		t.Error(err)
	}

	delivery, ok, err := amqpChan.Get("reporting.jobs.logs", true)
	if err != nil {
		t.Error(err)
	}
	if !ok {
		t.Error("expected log message, but there was none")
	}

	var lp amqpLogPart

	err = json.Unmarshal(delivery.Body, &lp)
	if err != nil {
		t.Error(err)
	}

	expected := amqpLogPart{
		JobID:   4,
		Content: "Hello, world!",
		Number:  0,
		UUID:    uuid.String(),
		Final:   false,
	}

	if expected != lp {
		t.Errorf("log part is %#v, expected %#v", lp, expected)
	}
}
Exemple #3
0
// Run starts the processor. This method will not return until the processor is
// terminated, either by calling the GracefulShutdown or Terminate methods, or
// if the build jobs channel is closed.
func (p *Processor) Run() {
	context.LoggerFromContext(p.ctx).Info("starting processor")
	defer context.LoggerFromContext(p.ctx).Info("processor done")

	for {
		select {
		case <-p.ctx.Done():
			context.LoggerFromContext(p.ctx).Info("processor is done, terminating")
			return
		case <-p.graceful:
			context.LoggerFromContext(p.ctx).Info("processor is done, terminating")
			return
		default:
		}

		select {
		case <-p.ctx.Done():
			context.LoggerFromContext(p.ctx).Info("processor is done, terminating")
			p.CurrentStatus = "done"
			return
		case <-p.graceful:
			context.LoggerFromContext(p.ctx).Info("processor is done, terminating")
			p.CurrentStatus = "done"
			return
		case buildJob, ok := <-p.buildJobsChan:
			if !ok {
				p.CurrentStatus = "done"
				return
			}

			hardTimeout := p.hardTimeout
			if buildJob.Payload().Timeouts.HardLimit != 0 {
				hardTimeout = time.Duration(buildJob.Payload().Timeouts.HardLimit) * time.Second
			}
			buildJob.StartAttributes().HardTimeout = hardTimeout

			ctx := context.FromJobID(context.FromRepository(p.ctx, buildJob.Payload().Repository.Slug), buildJob.Payload().Job.ID)
			if buildJob.Payload().UUID != "" {
				ctx = context.FromUUID(ctx, buildJob.Payload().UUID)
			}
			ctx, cancel := gocontext.WithTimeout(ctx, hardTimeout)
			p.LastJobID = buildJob.Payload().Job.ID
			p.CurrentStatus = "processing"
			p.process(ctx, buildJob)
			p.CurrentStatus = "waiting"
			cancel()
		}
	}
}
func TestAMQPMaxLogLength(t *testing.T) {
	amqpConn, amqpChan := setupConn(t)
	defer amqpConn.Close()
	defer amqpChan.Close()

	uuid := uuid.NewRandom()
	ctx := workerctx.FromUUID(context.TODO(), uuid.String())

	logWriter, err := newAMQPLogWriter(ctx, amqpConn, 4)
	if err != nil {
		t.Fatal(err)
	}
	logWriter.SetMaxLogLength(4)
	logWriter.SetTimeout(time.Second)

	_, err = fmt.Fprintf(logWriter, "1234")
	if err != nil {
		t.Error(err)
	}
	_, err = fmt.Fprintf(logWriter, "5")
	if err == nil {
		t.Error("expected error, but got nil")
	}
}