Created
August 25, 2014 06:47
-
-
Save imkira/f40e64d00583160c0c9f to your computer and use it in GitHub Desktop.
this exercises https://github.com/hashicorp/consul/issues/300
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
package main | |
import ( | |
"fmt" | |
"reflect" | |
"time" | |
"github.com/armon/consul-api" | |
) | |
func main() { | |
startRegisterChan := make(chan struct{}, 1) | |
killRegisterChan := make(chan struct{}, 1) | |
go registerLoop(startRegisterChan, killRegisterChan) | |
startLookupChan := make(chan struct{}, 1) | |
doneLookupChan := make(chan struct{}, 1) | |
go lookupLoop(startLookupChan, doneLookupChan) | |
fmt.Println("Running...") | |
close(startRegisterChan) | |
close(startLookupChan) | |
killRegisterChan <- <-doneLookupChan | |
} | |
func registerLoop(start <-chan struct{}, kill <-chan struct{}) { | |
agent := createClient().Agent() | |
reg := &consulapi.AgentServiceRegistration{ | |
ID: "testid", | |
Name: "testname", | |
Port: 12345, | |
Check: &consulapi.AgentServiceCheck{ | |
TTL: "10s", | |
}, | |
} | |
<-start | |
for { | |
select { | |
case <-kill: | |
return | |
default: | |
// loop until register succeeds | |
for agent.ServiceRegister(reg) != nil { | |
time.Sleep(10 * time.Millisecond) | |
} | |
time.Sleep(10 * time.Millisecond) | |
// loop until pass succeeds | |
for agent.PassTTL("service:testid", "i'm fine") != nil { | |
time.Sleep(10 * time.Millisecond) | |
} | |
time.Sleep(10 * time.Millisecond) | |
// loop until deregister succeeds | |
for agent.ServiceDeregister(reg.ID) != nil { | |
time.Sleep(10 * time.Millisecond) | |
} | |
} | |
} | |
} | |
func lookupLoop(start <-chan struct{}, done chan<- struct{}) { | |
defer close(done) | |
health := createClient().Health() | |
queryOpts := &consulapi.QueryOptions{ | |
RequireConsistent: true, | |
WaitTime: 1 * time.Millisecond, | |
} | |
<-start | |
var prevEntries []*consulapi.ServiceEntry | |
for i := 0; i < 100; i++ { | |
fmt.Printf("SEND: %d\n", queryOpts.WaitIndex) | |
curEntries, meta, err := health.Service("testname", "", false, queryOpts) | |
if err != nil { | |
time.Sleep(10 * time.Millisecond) | |
continue | |
} | |
fmt.Printf("GOT: %d\n", queryOpts.WaitIndex) | |
if len(curEntries) > 0 { | |
printEntry(curEntries[0]) | |
} | |
if queryOpts.WaitIndex != meta.LastIndex { | |
queryOpts.WaitIndex = meta.LastIndex | |
prevEntries = curEntries | |
continue | |
} | |
if len(prevEntries) != len(curEntries) { | |
fmt.Printf("ERR: len(Entries) mismatch %d vs %d (%d trials)\n", len(prevEntries), len(curEntries), i) | |
return | |
} | |
if len(curEntries) <= 0 { | |
continue | |
} | |
if reflect.DeepEqual(prevEntries, curEntries) == false { | |
fmt.Printf("ERR: DeepEqual mismatch (%d trials)\n", i) | |
printEntry(prevEntries[0]) | |
fmt.Printf("vs\n") | |
printEntry(curEntries[0]) | |
return | |
} | |
} | |
} | |
func printEntry(s *consulapi.ServiceEntry) { | |
for i, check := range s.Checks { | |
fmt.Printf("Checks[%d]: %#v\n", i, *check) | |
} | |
} |
Author
imkira
commented
Aug 26, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment