-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from micro/move-examples
- Loading branch information
Showing
31 changed files
with
4,482 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
|
||
"github.com/google/uuid" | ||
"github.com/micro/micro/v3/service" | ||
"github.com/micro/micro/v3/service/logger" | ||
"github.com/micro/micro/v3/service/store" | ||
) | ||
|
||
func main() { | ||
service.New( | ||
service.Name("blob-store"), | ||
service.Version("latest"), | ||
) | ||
|
||
key := uuid.New().String() | ||
|
||
buf := bytes.NewBuffer([]byte("world")) | ||
if err := store.DefaultBlobStore.Write(key, buf); err != nil { | ||
logger.Fatalf("Error writing to blob store: %v", err) | ||
} else { | ||
logger.Infof("Write okay") | ||
} | ||
|
||
res, err := store.DefaultBlobStore.Read(key) | ||
if err != nil { | ||
logger.Fatalf("Error reading from the blog store: %v", err) | ||
} | ||
bytes, err := ioutil.ReadAll(res) | ||
if err != nil { | ||
logger.Fatalf("Error reading result: %v", err) | ||
} | ||
logger.Infof("Read from blob store: %v", string(bytes)) | ||
|
||
if err := store.DefaultBlobStore.Delete(key); err != nil { | ||
logger.Fatalf("Error deleting from blob store: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/micro/micro/v3/service" | ||
"github.com/micro/micro/v3/service/config" | ||
) | ||
|
||
type keyConfig struct { | ||
Subkey string `json:"subkey"` | ||
Subkey1 int `json:"subkey1"` | ||
Subkey2 string `json:"subkey2"` | ||
} | ||
|
||
type conf struct { | ||
Key keyConfig `json:"key"` | ||
} | ||
|
||
func main() { | ||
// get a value | ||
go func() { | ||
for { | ||
time.Sleep(time.Second) | ||
// Test merge | ||
err := config.Set("key", map[string]interface{}{ | ||
"Subkey3": "Merge", | ||
}) | ||
if err != nil { | ||
fmt.Println("ERROR: ", err) | ||
} | ||
|
||
val, _ := config.Get("key.subkey3") | ||
if val.String("") != "Merge" { | ||
fmt.Println("ERROR: key.subkey3 should be 'Merge' but it is:", val.String("")) | ||
} | ||
|
||
val, err = config.Get("key.subkey") | ||
fmt.Println("Value of key.subkey: ", val.String(""), err) | ||
|
||
val, _ = config.Get("key", config.Secret(true)) | ||
c := conf{} | ||
err = val.Scan(&c.Key) | ||
fmt.Println("Value of key.subkey1: ", c.Key.Subkey1, err) | ||
fmt.Println("Value of key.subkey2: ", c.Key.Subkey2) | ||
|
||
val, _ = config.Get("key.subkey3") | ||
fmt.Println("Value of key.subkey3: ", val.String("")) | ||
|
||
// Test defaults | ||
val, _ = config.Get("key.subkey_does_not_exist") | ||
fmt.Println("Default", val.String("Hello")) | ||
} | ||
}() | ||
|
||
// run the service | ||
service.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package main | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
goevents "github.com/micro/go-micro/v3/events" | ||
"github.com/micro/micro/v3/service" | ||
"github.com/micro/micro/v3/service/events" | ||
"github.com/micro/micro/v3/service/logger" | ||
) | ||
|
||
type testPayload struct { | ||
Message string | ||
} | ||
|
||
var ( | ||
payload = testPayload{Message: "HelloWorld"} | ||
metadata = map[string]string{"foo": "bar"} | ||
) | ||
|
||
func main() { | ||
srv := service.New() | ||
srv.Init() | ||
|
||
go func() { // test 1, ordinary sub with autoack | ||
evChan, err := events.Subscribe("test1") | ||
if err != nil { | ||
logger.Fatalf("Error creating subscriber: %v", err) | ||
} | ||
// Is there a race condition here with publish coming straight after subscribe? Seems to be according to this | ||
// test so sleep for a bit to wait for nats to register subscription properly | ||
time.Sleep(2 * time.Second) | ||
|
||
logger.Infof("TEST1: publishing event") | ||
|
||
err = events.Publish("test1", payload, goevents.WithMetadata(metadata)) | ||
if err != nil { | ||
logger.Errorf("TEST1: Error publishing event: %v", err) | ||
return | ||
} | ||
|
||
logger.Infof("TEST1: waiting for event") | ||
event, ok := <-evChan | ||
if !ok { | ||
logger.Error("TEST1: Channel closed") | ||
return | ||
} | ||
|
||
if event.Topic != "test1" { | ||
logger.Errorf("TEST1: Incorrect topic: %v", event.Topic) | ||
return | ||
} | ||
if event.Metadata == nil || event.Metadata["foo"] != metadata["foo"] { | ||
logger.Errorf("TEST1: Incorrect metadata: %v", event.Metadata) | ||
return | ||
} | ||
|
||
var result testPayload | ||
if err := event.Unmarshal(&result); err != nil { | ||
logger.Errorf("TEST1: Error decoding result: %v. Payload was %v", err, string(event.Payload)) | ||
return | ||
} | ||
if result.Message != payload.Message { | ||
logger.Errorf("TEST1: Incorrect message: %v", result.Message) | ||
return | ||
} | ||
|
||
logger.Infof("TEST1: Finished ok") | ||
|
||
}() | ||
|
||
go func() { // test 2, sub with manual ack | ||
evChan, err := events.Subscribe("test2", goevents.WithAutoAck(false, 5*time.Second)) | ||
if err != nil { | ||
logger.Errorf("TEST2: Error creating subscriber: %v", err) | ||
return | ||
} | ||
|
||
// Is there a race condition here with publish coming straight after subscribe? Seems to be according to this | ||
// test so sleep for a bit to wait for nats to register subscription properly | ||
time.Sleep(2 * time.Second) | ||
|
||
logger.Infof("TEST2: publishing event") | ||
err = events.Publish("test2", payload, goevents.WithMetadata(metadata)) | ||
|
||
if err != nil { | ||
logger.Errorf("TEST2: Error publishing event: %v", err) | ||
return | ||
} | ||
|
||
logger.Infof("TEST2: waiting for event") | ||
event, ok := <-evChan | ||
if !ok { | ||
logger.Errorf("TEST2: Channel closed") | ||
return | ||
} | ||
|
||
if event.Topic != "test2" { | ||
logger.Errorf("TEST2: Incorrect topic: %v", event.Topic) | ||
return | ||
} | ||
if event.Metadata == nil || event.Metadata["foo"] != metadata["foo"] { | ||
logger.Errorf("TEST2: Incorrect metadata: %v", event.Metadata) | ||
return | ||
} | ||
|
||
var result testPayload | ||
if err := event.Unmarshal(&result); err != nil { | ||
logger.Errorf("TEST2: Error decoding result: %v. Payload was %v", err, string(event.Payload)) | ||
return | ||
} | ||
if result.Message != payload.Message { | ||
logger.Errorf("TEST2: Incorrect message: %v", result.Message) | ||
return | ||
} | ||
id := event.ID | ||
// nack the event to put it back on the queue | ||
logger.Infof("TEST2: Nacking the event") | ||
event.Nack() | ||
|
||
logger.Infof("TEST2: waiting for event") | ||
select { | ||
case event := <-evChan: | ||
if event.ID != id { | ||
logger.Errorf("Unexpected event received, expected %s, received %s", id, event.ID) | ||
return | ||
} | ||
logger.Infof("TEST2: Acking the event") | ||
event.Ack() | ||
|
||
case <-time.After(10 * time.Second): | ||
logger.Errorf("Timed out waiting for event") | ||
return | ||
} | ||
|
||
// we've acked so should receive nothing else | ||
logger.Infof("TEST2: Waiting for timeout") | ||
select { | ||
case event := <-evChan: | ||
logger.Errorf("Unexpected event received %s", event.ID) | ||
return | ||
case <-time.After(10 * time.Second): | ||
|
||
} | ||
|
||
logger.Infof("TEST2: Finished ok") | ||
|
||
}() | ||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
// wait indefinitely so this only runs once | ||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.