Skip to content
Permalink
Browse files

Subscription is now an UUID

  • Loading branch information...
infinytum committed Jan 15, 2019
1 parent fd65845 commit 48769206fe035989640d4e532702ebc2209babcc
Showing with 23 additions and 7 deletions.
  1. +1 −1 subject.go
  2. +1 −1 subject_test.go
  3. +8 −2 subscription.go
  4. +13 −3 subscription_test.go
@@ -114,7 +114,7 @@ func (subject *Subject) Subscribe(fn interface{}) (Subscription, error) {

return subscription, nil
}
return Subscription(""), errors.New("fn is not a function")
return EmptySubscription(), errors.New("fn is not a function")
}

// Unsubscribe unregisters a previously registered function for all
@@ -29,7 +29,7 @@ func TestSubject_Subscribe(t *testing.T) {
t.Error("Subscription is not in subscription map")
}

if sub, err := subject.Subscribe(3); err == nil || sub != "" {
if sub, err := subject.Subscribe(3); err == nil || sub != EmptySubscription() {
t.Error("Subscribe accepted non-function parameters")
}
}
@@ -4,9 +4,15 @@ import "github.com/google/uuid"

// Subscription represents a string to identify
// a subscription in an obserable so it can be removed
type Subscription string
type Subscription uuid.UUID

// NewSubscription generates a new subscription
func NewSubscription() Subscription {
return Subscription(uuid.New().String())
return Subscription(uuid.New())
}

func EmptySubscription() Subscription {
emptyBytes := make([]byte, 16)
emptyUuid, _ := uuid.ParseBytes(emptyBytes)
return Subscription(emptyUuid)
}
@@ -1,9 +1,19 @@
package reactive

import "testing"
import (
"testing"
)

func TestNewSubscription(t *testing.T) {
if NewSubscription() == "" {
t.Error("NewSubscription didnt return a new subscription id")
for i := 1; i == 10; i++ {
if NewSubscription() == NewSubscription() {
t.Error("Generated two identical subscription ids")
}
}
}

func TestEmptySubscription(t *testing.T) {
if EmptySubscription() != EmptySubscription() {
t.Error("Empty subscription is not constant")
}
}

0 comments on commit 4876920

Please sign in to comment.
You can’t perform that action at this time.