Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
fogfish committed Nov 18, 2023
1 parent d862572 commit 8366401
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
18 changes: 11 additions & 7 deletions internal/qtest/qtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ import (
const (
Category = "Note"
Message = "{\"some\":\"message\"}"
Event = ""
Receipt = "0x123456789abcdef"

EventCategory = "Event[Note]"
EventCategory = "qtest.EventNote"
)

var (
Expand All @@ -42,6 +41,11 @@ type User struct {
Some string `json:"some"`
}

type EventNote swarm.Event[*Note]

func (EventNote) HKT1(swarm.EventType) {}
func (EventNote) HKT2(*Note) {}

type effect = chan string
type queueName = string
type category = string
Expand Down Expand Up @@ -124,11 +128,11 @@ func TestEnqueueEvent(t *testing.T, factory enqueue) {

q := factory(eff, "test-queue", EventCategory, retry200ms)

note, _ := events.Enqueue[*Note, swarm.Event[*Note]](q)
note, _ := events.Enqueue[*Note, EventNote](q)
user, dlq := events.Enqueue[*User, swarm.Event[*User]](q)

t.Run("Enqueue", func(t *testing.T) {
note <- &swarm.Event[*Note]{
note <- &EventNote{
Object: &Note{Some: "message"},
}

Expand All @@ -140,7 +144,7 @@ func TestEnqueueEvent(t *testing.T, factory enqueue) {
it.Then(t).
Should(it.Nil(err)).
Should(it.Equal(*val.Object, Note{Some: "message"})).
Should(it.Equal(val.Type, "note:Event[Note]")).
Should(it.Equal(val.Type, "qtest.EventNote")).
ShouldNot(it.Equal(len(val.ID), 0)).
ShouldNot(it.True(val.Created.IsZero()))

Expand Down Expand Up @@ -231,7 +235,7 @@ func TestDequeueEvent(t *testing.T, factory dequeue) {
eff := make(chan string, 1)

t.Run("Typed", func(t *testing.T) {
event := swarm.Event[*Note]{
event := &EventNote{
ID: "id",
Type: "type",
Agent: "agent",
Expand All @@ -243,7 +247,7 @@ func TestDequeueEvent(t *testing.T, factory dequeue) {

q := factory(eff, "test-queue", EventCategory, string(message), Receipt, retry200ms)

msg, ack := events.Dequeue[*Note, swarm.Event[*Note]](q)
msg, ack := events.Dequeue[*Note, EventNote](q)
go q.Await()

val := <-msg
Expand Down
9 changes: 5 additions & 4 deletions queue/events/enqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func Enqueue[T any, E swarm.EventKind[T]](q swarm.Broker, category ...string) (c
conf := q.Config()
ch := swarm.NewEvtEnqCh[T, E](conf.EnqueueCapacity)

catT := strings.ToLower(categoryOf[T]())
catE := categoryOf[E]()
if len(category) > 0 {
catE = category[0]
Expand All @@ -45,7 +44,7 @@ func Enqueue[T any, E swarm.EventKind[T]](q swarm.Broker, category ...string) (c

_, knd, src, _ := shape.Get(object)
if knd == "" {
knd = curie.IRI(catT + ":" + catE)
knd = curie.IRI(catE)
}

if src == "" {
Expand Down Expand Up @@ -83,9 +82,11 @@ func Enqueue[T any, E swarm.EventKind[T]](q swarm.Broker, category ...string) (c
// normalized type name
func categoryOf[T any]() string {
typ := reflect.TypeOf(new(T)).Elem()
cat := typ.String()
if typ.Kind() == reflect.Ptr {
return typ.Elem().String()
cat = typ.Elem().String()
}

return typ.String()
seq := strings.Split(cat, "[")
return seq[0]
}

0 comments on commit 8366401

Please sign in to comment.