Ejemplo n.º 1
0
// Exec runs the use case.
func (kase *ProcessDelivery) Exec(
	ctx context.Context,
	d entity.Delivery,
) (err error) {

	err = d.Valid()
	if err != nil {
		return
	}

	var (
		token    int64
		deadline time.Time
	)

	token, deadline, err = kase.DB.StartDelivery(d)
	if err != nil {
		return
	}

	var cancel func()
	ctx, cancel = context.WithDeadline(ctx, deadline)
	defer cancel()

	select {
	case err = <-kase.performDelivery(d):
		if err != nil {
			// TODO: write warning if mark failed
			kase.DB.MarkDeliveryFailed(token, d)
			return err
		}

		return kase.DB.MarkDeliverySuccess(token, d)
	case <-ctx.Done():
		// our deadline expired or we were canceled, so abandon the delivery
		err = errors.New("deadline exceeded")
		return
	}
}
Ejemplo n.º 2
0
func (db *Driver) saveDelivery(tx *sqlx.Tx, d *entity.Delivery) error {
	var row Delivery
	err := row.ScanInsert(*d)
	if err != nil {
		return err
	}

	sql, args, err := db.DB.BindNamed(Queries.Delivery.Insert, &row)
	if err != nil {
		return err
	}

	err = db.DB.Get(&row, sql, args...)
	if err != nil {
		return err
	}

	d.ID = &usecase.RepoID{T: "delivery", V: row.ID}
	return nil
}
Ejemplo n.º 3
0
	// "errors"
	//
	"github.com/nullstyle/coinop/entity"
	"github.com/nullstyle/coinop/fake"
	"github.com/nullstyle/coinop/test"
	. "github.com/nullstyle/coinop/usecase"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	// "github.com/stretchr/testify/mock"
	"golang.org/x/net/context"
)

var _ = Describe("ProcessDelivery", func() {
	var (
		repo     *MockDeliveryRepository
		send     *MockDeliverySender
		delivery entity.Delivery
		err      error
	)

	BeforeEach(func() {
		repo = &MockDeliveryRepository{}
		send = &MockDeliverySender{}
	})

	AfterEach(func() {
		test.VerifyMock(repo.Mock, send.Mock)
	})

	JustBeforeEach(func() {
		subject := &ProcessDelivery{repo, send}
		err = subject.Exec(context.Background(), delivery)