-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
254 additions
and
43 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
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
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,114 @@ | ||
package multitenant | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
"testing" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"github.com/weaveworks/scope/app" | ||
"github.com/weaveworks/scope/common/xfer" | ||
"github.com/weaveworks/scope/probe/appclient" | ||
) | ||
|
||
type adapter struct { | ||
c appclient.AppClient | ||
} | ||
|
||
func (a adapter) PipeConnection(_, pipeID string, pipe xfer.Pipe) error { | ||
a.c.PipeConnection(pipeID, pipe) | ||
return nil | ||
} | ||
|
||
func (a adapter) PipeClose(_, pipeID string) error { | ||
return a.c.PipeClose(pipeID) | ||
} | ||
|
||
func doPipe(t *testing.T, pr1, pr2 app.PipeRouter) { | ||
|
||
} | ||
|
||
type pipeconn struct { | ||
id string | ||
uiPR, probePR app.PipeRouter | ||
uiPipe, probePipe xfer.Pipe | ||
uiIO, probeIO io.ReadWriter | ||
} | ||
|
||
func TestPipeRouter(t *testing.T) { | ||
var ( | ||
consul = newMockConsulClient() | ||
replicas = 2 | ||
iterations = 100 | ||
prs = []app.PipeRouter{} | ||
pipes = []pipeconn{} | ||
) | ||
|
||
for i := 0; i < replicas; i++ { | ||
pr := NewConsulPipeRouter(consul, "", fmt.Sprintf("127.0.0.1:44%02d", i), NoopUserIDer) | ||
defer pr.Stop() | ||
prs = append(prs, pr) | ||
} | ||
|
||
newPipe := func(i int) { | ||
// make a new pipe id | ||
id := fmt.Sprintf("pipe-%d", rand.Int63()) | ||
|
||
// pick a random PR to connect app to | ||
uiPR := prs[rand.Intn(replicas)] | ||
uiPipe, uiIO, err := uiPR.Get(context.Background(), id, app.UIEnd) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// pick a random PR to connect probe to | ||
probePR := prs[rand.Intn(replicas)] | ||
probePipe, probeIO, err := probePR.Get(context.Background(), id, app.ProbeEnd) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
pipes = append(pipes, pipeconn{ | ||
id: id, | ||
uiPR: uiPR, | ||
uiPipe: uiPipe, | ||
uiIO: uiIO, | ||
probePR: probePR, | ||
probePipe: probePipe, | ||
probeIO: probeIO, | ||
}) | ||
} | ||
|
||
deletePipe := func() { | ||
// pick a random pipe | ||
i := rand.Intn(len(pipes)) | ||
pipe := pipes[i] | ||
|
||
if err := pipe.uiPR.Release(context.Background(), pipe.id, app.UIEnd); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := pipe.probePR.Release(context.Background(), pipe.id, app.ProbeEnd); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// remove from list | ||
pipes = pipes[:i+copy(pipes[i:], pipes[i+1:])] | ||
} | ||
|
||
for i := 0; i < iterations; i++ { | ||
if len(pipes) <= 0 { | ||
newPipe(i) | ||
continue | ||
} | ||
r := rand.Float32() | ||
switch { | ||
case r < 0.5: | ||
newPipe(i) | ||
case r > 0.5: | ||
deletePipe() | ||
} | ||
} | ||
} |
Oops, something went wrong.