func (h *Hospital) ConsumeQueuesWithLimit(ctx context.Context, t *testing.T, limit int, failIfError bool) (events int, messages []string) {
	t.Helper()
	for h.HasEvents() {
		ran, err := h.RunNextEventIfDue(ctx)
		if err != nil && failIfError {
			t.Fatalf("hospital.RunNextEventIfDue() failed with %v", err)
		}
		messages = append(messages, h.consumeDueMessages(t, failIfError)...)
		if ran {
			events++
		} else {
			h.clock.Advance(defaultClockTick)
			messages = append(messages, h.consumeDueMessages(t, failIfError)...)
		}
		if limit > 0 && limit >= events {
			return
		}
	}
	messages = append(messages, h.consumeAllMessages(t, failIfError)...)
	return
}
ConsumeQueuesWithLimit consumes limit events and their corresponding messages, and returns the number of events that were run
and the messages that were sent.
Parameters:
* limit is the maximum number of events to run. All messages relevant to those events will be sent.
If there are less than limit events in the queue, this method consumes the events in the queue and returns. Set <1 for no limit.
* 'failIfError' specifies whether errors running events or sending messages should make the method fail.
The order in which events and messages are processed is the following:
1. Run all events that are due at the current time.
2. Send all messages that are due at the current time.
3. If there were no events due, advance the clock, and process any messages for that time so that we give priority
to the existing messages before running new events.
4. If there are still events in the queue, go back to step 1.
5. After all events are consumed, process all of the remaining messages advancing the clock if needed.
package testhospital
import (
	"context"
	"testing"
	"time"
	"github.com/google/simhospital/pkg/config"
	"github.com/google/simhospital/pkg/hospital"
	"github.com/google/simhospital/pkg/location"
	"github.com/google/simhospital/pkg/pathway"
	"github.com/google/simhospital/pkg/test"
	"github.com/google/simhospital/pkg/test/testaddress"
	"github.com/google/simhospital/pkg/test/testclock"
	"github.com/google/simhospital/pkg/test/testhl7"
	"github.com/google/simhospital/pkg/test/testid"
	"github.com/google/simhospital/pkg/test/testresource"
)
const defaultClockTick = time.Second
var (
	now           = time.Date(2018, 2, 12, 0, 0, 0, 0, time.UTC)
	dataFilesTest = test.DataFiles[test.Test]
	Arguments = hospital.Arguments{
		DoctorsFile:          &test.DoctorsConfigTest,
		OrderProfilesFile:    &test.OrderProfilesConfigTest,
		PathwayArguments:     &hospital.PathwayArguments{Dir: test.PathwaysDirTest, Type: "distribution"},
		Hl7ConfigFile:        &test.MessageConfigTest,
		HeaderConfigFile:     &test.HeaderConfigTest,
		HardcodedMessagesDir: &test.HardcodedMessagesDirTest,
		LocationsFile:        &test.LocationsConfigTest,
		DataFiles:            &dataFilesTest,
	}
)
type Hospital struct {
	*hospital.Hospital
	clock           *testclock.Clock
	Sender          *testhl7.Sender
	Parser          *pathway.Parser
	PathwayManager  pathway.Manager
	LocationManager *location.Manager
	MessageConfig   *config.HL7Config
}
type Config struct {
	hospital.Config
	hospital.Arguments
}
func New(ctx context.Context, t *testing.T, cfg Config) *Hospital {
	t.Helper()
	return WithTime(ctx, t, cfg, now)
}
func WithTime(ctx context.Context, t *testing.T, cfg Config, now time.Time) *Hospital {
	t.Helper()
	clock := testclock.New(now)
	cfg.Arguments.MessageControlGenerator = cfg.Config.MessageControlGenerator
	cfg.Arguments.Clock = clock
	cfg.Arguments.DeletePatientsFromMemory = cfg.Config.DeletePatientsFromMemory
	if cfg.Config.DataFiles != (config.DataFiles{}) {
		cfg.Arguments.DataFiles = &cfg.Config.DataFiles
	}
	c, err := hospital.DefaultConfig(ctx, cfg.Arguments)
	if err != nil {
		t.Fatalf("hospital.DefaultConfig(%+v) failed with %v", cfg.Arguments, err)
	}
	if cfg.LocationManager != nil {
		c.LocationManager = cfg.LocationManager
	}
	if cfg.MessagesManager != nil {
		c.MessagesManager = cfg.MessagesManager
	}
	if cfg.HL7Config != nil {
		c.HL7Config = cfg.HL7Config
	}
	if cfg.Header != nil {
		c.Header = cfg.Header
	}
	if cfg.Doctors != nil {
		c.Doctors = cfg.Doctors
	}
	if cfg.OrderProfiles != nil {
		c.OrderProfiles = cfg.OrderProfiles
	}
	if cfg.PathwayManager != nil {
		c.PathwayManager = cfg.PathwayManager
	}
	if cfg.Sender != nil {
		c.Sender = cfg.Sender
	} else {
		c.Sender = &testhl7.Sender{}
	}
	if cfg.ResourceWriter != nil {
		c.ResourceWriter = cfg.ResourceWriter
	} else {
		c.ResourceWriter = testresource.NewWriter()
	}
	c.AdditionalConfig = cfg.AdditionalConfig
	c.AdditionalConfig.AddressGenerator = &testaddress.ArbitraryGenerator
	c.AdditionalConfig.MRNGenerator = &testid.Generator{}
	c.AdditionalConfig.PlacerGenerator = &testid.Generator{}
	c.AdditionalConfig.FillerGenerator = &testid.Generator{}
	h, err := hospital.NewHospital(ctx, c)
	if err != nil {
		t.Fatalf("NewHospital(%+v) failed with %v", c, err)
	}
	return &Hospital{
		Hospital:        h,
		clock:           c.Clock.(*testclock.Clock),
		Sender:          c.Sender.(*testhl7.Sender),
		Parser:          c.PathwayParser,
		PathwayManager:  c.PathwayManager,
		LocationManager: c.LocationManager,
		MessageConfig:   c.HL7Config,
	}
}
func (h *Hospital) ConsumeQueues(ctx context.Context, t *testing.T) (events int, messages []string) {
	t.Helper()
	return h.ConsumeQueuesWithLimit(ctx, t, -1, true)
}