Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sejongk committed Sep 7, 2023
1 parent 6dc5b7b commit de2ee7d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ var (
// ErrReservedEventType is returned when the event type is reserved.
ErrReservedEventType = errors.New(
"the event type is reserved for a different use case")

// ErrUnsupportedPayloadType is returned when the payload is unserializable to JSON.
ErrUnsupportedPayloadType = errors.New(
"the payload is an unsupported JSON type")
)

// DocEvent represents the event that occurred in the document.
Expand Down Expand Up @@ -390,7 +394,7 @@ func (d *Document) Broadcast(eventType string, payload any) error {

marshaled, err := gojson.Marshal(payload)
if err != nil {
return fmt.Errorf("marshal payload in broadcast event: %w", err)
return ErrUnsupportedPayloadType
}

d.broadcastRequests <- BroadcastRequest{
Expand Down Expand Up @@ -462,8 +466,5 @@ func messageFromMsgAndArgs(msgAndArgs ...interface{}) string {
}

func isEventTypeReserved(eventType string) bool {
if eventType == "document" {
return true
}
return false
return eventType == "document"
}
72 changes: 72 additions & 0 deletions test/integration/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,78 @@ func TestDocument(t *testing.T) {
wg.Wait()
})

t.Run("unsubscriber can broadcast", func(t *testing.T) {
bch := make(chan string)
ctx := context.Background()
handler := func(eventType, publisher string, payload []byte) error {
var mentionedBy string
assert.NoError(t, gojson.Unmarshal(payload, &mentionedBy))
// Send the unmarshaled payload to the channel to notify that this
// subscriber receives the event.
bch <- mentionedBy
return nil
}

d1 := document.New(helper.TestDocKey(t))
assert.NoError(t, c1.Attach(ctx, d1))
rch1, err := c1.Watch(ctx, d1)
assert.NoError(t, err)
err = d1.SubscribeBroadcastEvent("mention", handler)
assert.NoError(t, err)

// c2 doesn't subscribe to the "mention" broadcast event.
d2 := document.New(helper.TestDocKey(t))
assert.NoError(t, c2.Attach(ctx, d2))
rch2, err := c2.Watch(ctx, d2)
assert.NoError(t, err)

// The unsubscriber c2 broadcasts the "mention" event.
err = d2.Broadcast("mention", "yorkie")
assert.NoError(t, err)

wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
rcv := 0
for {
select {
case <-rch1:
case <-rch2:
case m := <-bch:
assert.Equal(t, "yorkie", m)
rcv++
case <-time.After(1 * time.Second):
// Assuming that all subscribers can receive the broadcast
// event within this timeout period, check if the subscriber
// receives the unsubscriber's event.
assert.Equal(t, 1, rcv)
return
case <-ctx.Done():
return
}
}
}()

wg.Wait()
})

t.Run("reject to broadcast unserializable payload", func(t *testing.T) {
ctx := context.Background()

d1 := document.New(helper.TestDocKey(t))
assert.NoError(t, c1.Attach(ctx, d1))
_, err := c1.Watch(ctx, d1)
assert.NoError(t, err)
err = d1.SubscribeBroadcastEvent("mention", nil)
assert.NoError(t, err)

// Try to broadcast an unserializable payload.
ch := make(chan string)
err = d1.Broadcast("mention", ch)
assert.ErrorIs(t, document.ErrUnsupportedPayloadType, err)
})

}

func TestDocumentWithProjects(t *testing.T) {
Expand Down

1 comment on commit de2ee7d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go Benchmark

Benchmark suite Current: de2ee7d Previous: 8b2a9a7 Ratio
BenchmarkDocument/constructor_test - ns/op 1947 ns/op 1645 ns/op 1.18
BenchmarkDocument/constructor_test - B/op 1144 B/op 1144 B/op 1
BenchmarkDocument/constructor_test - allocs/op 18 allocs/op 18 allocs/op 1
BenchmarkDocument/status_test - ns/op 1199 ns/op 1036 ns/op 1.16
BenchmarkDocument/status_test - B/op 1112 B/op 1112 B/op 1
BenchmarkDocument/status_test - allocs/op 16 allocs/op 16 allocs/op 1
BenchmarkDocument/equals_test - ns/op 14866 ns/op 9056 ns/op 1.64
BenchmarkDocument/equals_test - B/op 6721 B/op 6720 B/op 1.00
BenchmarkDocument/equals_test - allocs/op 113 allocs/op 113 allocs/op 1
BenchmarkDocument/nested_update_test - ns/op 27238 ns/op 23725 ns/op 1.15
BenchmarkDocument/nested_update_test - B/op 11897 B/op 11897 B/op 1
BenchmarkDocument/nested_update_test - allocs/op 251 allocs/op 251 allocs/op 1
BenchmarkDocument/delete_test - ns/op 34804 ns/op 29089 ns/op 1.20
BenchmarkDocument/delete_test - B/op 15123 B/op 15122 B/op 1.00
BenchmarkDocument/delete_test - allocs/op 330 allocs/op 330 allocs/op 1
BenchmarkDocument/object_test - ns/op 12532 ns/op 10382 ns/op 1.21
BenchmarkDocument/object_test - B/op 6656 B/op 6657 B/op 1.00
BenchmarkDocument/object_test - allocs/op 113 allocs/op 113 allocs/op 1
BenchmarkDocument/array_test - ns/op 42219 ns/op 34925 ns/op 1.21
BenchmarkDocument/array_test - B/op 11753 B/op 11754 B/op 1.00
BenchmarkDocument/array_test - allocs/op 267 allocs/op 267 allocs/op 1
BenchmarkDocument/text_test - ns/op 47024 ns/op 38657 ns/op 1.22
BenchmarkDocument/text_test - B/op 14826 B/op 14826 B/op 1
BenchmarkDocument/text_test - allocs/op 473 allocs/op 473 allocs/op 1
BenchmarkDocument/text_composition_test - ns/op 45685 ns/op 38208 ns/op 1.20
BenchmarkDocument/text_composition_test - B/op 18211 B/op 18210 B/op 1.00
BenchmarkDocument/text_composition_test - allocs/op 474 allocs/op 474 allocs/op 1
BenchmarkDocument/rich_text_test - ns/op 120669 ns/op 102400 ns/op 1.18
BenchmarkDocument/rich_text_test - B/op 37024 B/op 37016 B/op 1.00
BenchmarkDocument/rich_text_test - allocs/op 1134 allocs/op 1134 allocs/op 1
BenchmarkDocument/counter_test - ns/op 25556 ns/op 21823 ns/op 1.17
BenchmarkDocument/counter_test - B/op 10177 B/op 10178 B/op 1.00
BenchmarkDocument/counter_test - allocs/op 238 allocs/op 238 allocs/op 1
BenchmarkDocument/text_edit_gc_100 - ns/op 4721829 ns/op 3919058 ns/op 1.20
BenchmarkDocument/text_edit_gc_100 - B/op 1553489 B/op 1553500 B/op 1.00
BenchmarkDocument/text_edit_gc_100 - allocs/op 17164 allocs/op 17166 allocs/op 1.00
BenchmarkDocument/text_edit_gc_1000 - ns/op 374136806 ns/op 305822693 ns/op 1.22
BenchmarkDocument/text_edit_gc_1000 - B/op 136640184 B/op 136666284 B/op 1.00
BenchmarkDocument/text_edit_gc_1000 - allocs/op 210694 allocs/op 210879 allocs/op 1.00
BenchmarkDocument/text_split_gc_100 - ns/op 5395264 ns/op 4461948 ns/op 1.21
BenchmarkDocument/text_split_gc_100 - B/op 2217601 B/op 2218207 B/op 1.00
BenchmarkDocument/text_split_gc_100 - allocs/op 16591 allocs/op 16595 allocs/op 1.00
BenchmarkDocument/text_split_gc_1000 - ns/op 432990931 ns/op 357452373 ns/op 1.21
BenchmarkDocument/text_split_gc_1000 - B/op 214863149 B/op 214840861 B/op 1.00
BenchmarkDocument/text_split_gc_1000 - allocs/op 211476 allocs/op 211342 allocs/op 1.00
BenchmarkDocument/text_delete_all_10000 - ns/op 21712924 ns/op 17519738 ns/op 1.24
BenchmarkDocument/text_delete_all_10000 - B/op 5904939 B/op 5905240 B/op 1.00
BenchmarkDocument/text_delete_all_10000 - allocs/op 41127 allocs/op 41131 allocs/op 1.00
BenchmarkDocument/text_delete_all_100000 - ns/op 297894252 ns/op 239749099 ns/op 1.24
BenchmarkDocument/text_delete_all_100000 - B/op 53832542 B/op 53858656 B/op 1.00
BenchmarkDocument/text_delete_all_100000 - allocs/op 415919 allocs/op 416083 allocs/op 1.00
BenchmarkDocument/text_100 - ns/op 382505 ns/op 313643 ns/op 1.22
BenchmarkDocument/text_100 - B/op 118419 B/op 118421 B/op 1.00
BenchmarkDocument/text_100 - allocs/op 5077 allocs/op 5077 allocs/op 1
BenchmarkDocument/text_1000 - ns/op 4163660 ns/op 3457317 ns/op 1.20
BenchmarkDocument/text_1000 - B/op 1153040 B/op 1153025 B/op 1.00
BenchmarkDocument/text_1000 - allocs/op 50081 allocs/op 50081 allocs/op 1
BenchmarkDocument/array_1000 - ns/op 2059827 ns/op 1713545 ns/op 1.20
BenchmarkDocument/array_1000 - B/op 1103090 B/op 1103084 B/op 1.00
BenchmarkDocument/array_1000 - allocs/op 11871 allocs/op 11871 allocs/op 1
BenchmarkDocument/array_10000 - ns/op 23442081 ns/op 19152387 ns/op 1.22
BenchmarkDocument/array_10000 - B/op 9908069 B/op 9906093 B/op 1.00
BenchmarkDocument/array_10000 - allocs/op 120725 allocs/op 120718 allocs/op 1.00
BenchmarkDocument/array_gc_100 - ns/op 215174 ns/op 176825 ns/op 1.22
BenchmarkDocument/array_gc_100 - B/op 98354 B/op 98364 B/op 1.00
BenchmarkDocument/array_gc_100 - allocs/op 1246 allocs/op 1246 allocs/op 1
BenchmarkDocument/array_gc_1000 - ns/op 2323136 ns/op 1937102 ns/op 1.20
BenchmarkDocument/array_gc_1000 - B/op 1170404 B/op 1170600 B/op 1.00
BenchmarkDocument/array_gc_1000 - allocs/op 12908 allocs/op 12909 allocs/op 1.00
BenchmarkDocument/counter_1000 - ns/op 341080 ns/op 289151 ns/op 1.18
BenchmarkDocument/counter_1000 - B/op 198741 B/op 198740 B/op 1.00
BenchmarkDocument/counter_1000 - allocs/op 6506 allocs/op 6506 allocs/op 1
BenchmarkDocument/counter_10000 - ns/op 3678929 ns/op 3069618 ns/op 1.20
BenchmarkDocument/counter_10000 - B/op 2165646 B/op 2165685 B/op 1.00
BenchmarkDocument/counter_10000 - allocs/op 69513 allocs/op 69513 allocs/op 1
BenchmarkDocument/object_1000 - ns/op 2236870 ns/op 1774782 ns/op 1.26
BenchmarkDocument/object_1000 - B/op 1451276 B/op 1451444 B/op 1.00
BenchmarkDocument/object_1000 - allocs/op 9917 allocs/op 9918 allocs/op 1.00
BenchmarkDocument/object_10000 - ns/op 26439005 ns/op 22137040 ns/op 1.19
BenchmarkDocument/object_10000 - B/op 12367961 B/op 12370116 B/op 1.00
BenchmarkDocument/object_10000 - allocs/op 101220 allocs/op 101224 allocs/op 1.00
BenchmarkDocument/tree_100 - ns/op 1151867 ns/op
BenchmarkDocument/tree_100 - B/op 442825 B/op
BenchmarkDocument/tree_100 - allocs/op 4503 allocs/op
BenchmarkDocument/tree_1000 - ns/op 79766724 ns/op
BenchmarkDocument/tree_1000 - B/op 35223398 B/op
BenchmarkDocument/tree_1000 - allocs/op 44115 allocs/op
BenchmarkDocument/tree_10000 - ns/op 10667172983 ns/op
BenchmarkDocument/tree_10000 - B/op 3438899104 B/op
BenchmarkDocument/tree_10000 - allocs/op 440160 allocs/op
BenchmarkDocument/tree_delete_all_1000 - ns/op 79992912 ns/op
BenchmarkDocument/tree_delete_all_1000 - B/op 35693296 B/op
BenchmarkDocument/tree_delete_all_1000 - allocs/op 51767 allocs/op
BenchmarkDocument/tree_edit_gc_100 - ns/op 4594580 ns/op
BenchmarkDocument/tree_edit_gc_100 - B/op 2077363 B/op
BenchmarkDocument/tree_edit_gc_100 - allocs/op 11162 allocs/op
BenchmarkDocument/tree_edit_gc_1000 - ns/op 304530487 ns/op
BenchmarkDocument/tree_edit_gc_1000 - B/op 180290894 B/op
BenchmarkDocument/tree_edit_gc_1000 - allocs/op 113373 allocs/op
BenchmarkDocument/tree_split_gc_100 - ns/op 2877198 ns/op
BenchmarkDocument/tree_split_gc_100 - B/op 1347822 B/op
BenchmarkDocument/tree_split_gc_100 - allocs/op 9058 allocs/op
BenchmarkDocument/tree_split_gc_1000 - ns/op 191127626 ns/op
BenchmarkDocument/tree_split_gc_1000 - B/op 113955840 B/op
BenchmarkDocument/tree_split_gc_1000 - allocs/op 93868 allocs/op
BenchmarkRPC/client_to_server - ns/op 485469293 ns/op 403586987 ns/op 1.20
BenchmarkRPC/client_to_server - B/op 12327624 B/op 12224802 B/op 1.01
BenchmarkRPC/client_to_server - allocs/op 177233 allocs/op 176918 allocs/op 1.00
BenchmarkRPC/client_to_client_via_server - ns/op 805328426 ns/op 703134840 ns/op 1.15
BenchmarkRPC/client_to_client_via_server - B/op 22974172 B/op 22618076 B/op 1.02
BenchmarkRPC/client_to_client_via_server - allocs/op 331713 allocs/op 331134 allocs/op 1.00
BenchmarkRPC/attach_large_document - ns/op 1864077309 ns/op 1359748878 ns/op 1.37
BenchmarkRPC/attach_large_document - B/op 1810323952 B/op 1799405880 B/op 1.01
BenchmarkRPC/attach_large_document - allocs/op 9386 allocs/op 9590 allocs/op 0.98
BenchmarkRPC/adminCli_to_server - ns/op 675480541 ns/op 598842152 ns/op 1.13
BenchmarkRPC/adminCli_to_server - B/op 21105672 B/op 21103788 B/op 1.00
BenchmarkRPC/adminCli_to_server - allocs/op 333635 allocs/op 333606 allocs/op 1.00
BenchmarkLocker - ns/op 169 ns/op 122.4 ns/op 1.38
BenchmarkLocker - B/op 16 B/op 16 B/op 1
BenchmarkLocker - allocs/op 1 allocs/op 1 allocs/op 1
BenchmarkLockerParallel - ns/op 156.8 ns/op 128.7 ns/op 1.22
BenchmarkLockerParallel - B/op 0 B/op 0 B/op NaN
BenchmarkLockerParallel - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkLockerMoreKeys - ns/op 507.1 ns/op 483.6 ns/op 1.05
BenchmarkLockerMoreKeys - B/op 13 B/op 13 B/op 1
BenchmarkLockerMoreKeys - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkSync/memory_sync_10_test - ns/op 8716 ns/op 7322 ns/op 1.19
BenchmarkSync/memory_sync_10_test - B/op 1282 B/op 1283 B/op 1.00
BenchmarkSync/memory_sync_10_test - allocs/op 38 allocs/op 38 allocs/op 1
BenchmarkSync/memory_sync_100_test - ns/op 79535 ns/op 69765 ns/op 1.14
BenchmarkSync/memory_sync_100_test - B/op 8671 B/op 8737 B/op 0.99
BenchmarkSync/memory_sync_100_test - allocs/op 275 allocs/op 279 allocs/op 0.99
BenchmarkSync/memory_sync_1000_test - ns/op 768720 ns/op 686582 ns/op 1.12
BenchmarkSync/memory_sync_1000_test - B/op 81855 B/op 81819 B/op 1.00
BenchmarkSync/memory_sync_1000_test - allocs/op 2581 allocs/op 2582 allocs/op 1.00
BenchmarkSync/memory_sync_10000_test - ns/op 7836295 ns/op 7139141 ns/op 1.10
BenchmarkSync/memory_sync_10000_test - B/op 859798 B/op 858288 B/op 1.00
BenchmarkSync/memory_sync_10000_test - allocs/op 26648 allocs/op 26871 allocs/op 0.99
BenchmarkTextEditing - ns/op 31000687882 ns/op 27047402085 ns/op 1.15
BenchmarkTextEditing - B/op 8456842736 B/op 8456806024 B/op 1.00
BenchmarkTextEditing - allocs/op 20615354 allocs/op 20613838 allocs/op 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.