diff --git a/context/context.go b/context/context.go index 9893029..7b0391f 100644 --- a/context/context.go +++ b/context/context.go @@ -16,79 +16,90 @@ type Context interface { GetBool(k string) (bool, bool) MarshalJSON() ([]byte, error) UnmarshalJSON(b []byte) error - Clone() contextMap + Clone() Context } -type contextMap map[string]interface{} +type contextMap struct { + c map[string]interface{} + index int +} func New() Context { - var c contextMap = make(contextMap) + var c = contextMap{ + c: make(map[string]interface{}), + index: 0, + } return &c } -func (c contextMap) PutString(k string, v string) { - c[k] = v +func (c *contextMap) PutString(k string, v string) { + c.c[k] = v } -func (c contextMap) GetString(k string) (string, bool) { - if c[k] == nil { +func (c *contextMap) GetString(k string) (string, bool) { + if c.c[k] == nil { return "", false } else { - return c[k].(string), true + return c.c[k].(string), true } } -func (c contextMap) PutInt(k string, v int) { +func (c *contextMap) PutInt(k string, v int) { // saves as string, avoids json.Marshal turning int into json.numbers(float64) - c[k] = strconv.Itoa(v) + c.c[k] = strconv.Itoa(v) } -func (c contextMap) GetInt(k string) (int, bool) { - if c[k] == nil { +func (c *contextMap) GetInt(k string) (int, bool) { + if c.c[k] == nil { return 0, false } else { - value, _ := strconv.ParseInt(c[k].(string), 0, 0) + value, _ := strconv.ParseInt(c.c[k].(string), 0, 0) return int(value), true } } -func (c contextMap) PutFloat64(k string, v float64) { - c[k] = v +func (c *contextMap) PutFloat64(k string, v float64) { + c.c[k] = v } -func (c contextMap) GetFloat64(k string) (float64, bool) { - if c[k] == nil { +func (c *contextMap) GetFloat64(k string) (float64, bool) { + if c.c[k] == nil { return 0, false } else { - return c[k].(float64), true + return c.c[k].(float64), true } } -func (c contextMap) PutBool(k string, v bool) { - c[k] = v +func (c *contextMap) PutBool(k string, v bool) { + c.c[k] = v } -func (c contextMap) GetBool(k string) (bool, bool) { - if c[k] == nil { +func (c *contextMap) GetBool(k string) (bool, bool) { + if c.c[k] == nil { return false, false } else { - return c[k].(bool), true + return c.c[k].(bool), true } } -func (c contextMap) MarshalJSON() ([]byte, error) { - return json.Marshal(map[string]interface{}(c)) +func (c *contextMap) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]interface{}(c.c)) } -func (c contextMap) UnmarshalJSON(b []byte) error { - var m map[string]interface{} = c +func (c *contextMap) UnmarshalJSON(b []byte) error { + var m map[string]interface{} = c.c return json.Unmarshal(b, &m) } -func (c contextMap) Clone() contextMap { - var clone = make(contextMap) - for k, v := range c { - clone[k] = v +func (c *contextMap) Clone() Context { + var clone = contextMap{ + c: make(map[string]interface{}), } - return clone + for k, v := range c.c { + clone.c[k] = v + } + + clone.PutInt("workerIndex", c.index) + c.index++ + return &clone } diff --git a/context/context_test.go b/context/context_test.go index 13936c6..4c94e81 100644 --- a/context/context_test.go +++ b/context/context_test.go @@ -74,6 +74,18 @@ var _ = Describe("Context map", func() { result, _ = cloned.GetString("str1") Ω(result).Should(Equal("abc")) }) + It("record workerIndex", func() { + + cloned := localContext.Clone() + index, exists := cloned.GetInt("workerIndex") + Ω(exists).ShouldNot(BeFalse()) + Ω(index).Should(Equal(0)) + cloned = localContext.Clone() + index, exists = cloned.GetInt("workerIndex") + Ω(exists).ShouldNot(BeFalse()) + Ω(index).Should(Equal(1)) + + }) }) }) diff --git a/workloads/rest.go b/workloads/rest.go index cd1991e..f77a31d 100644 --- a/workloads/rest.go +++ b/workloads/rest.go @@ -56,9 +56,9 @@ func (r *rest) Target(ctx context.Context) error { func (r *rest) Login(ctx context.Context) error { body := &LoginResponse{} - iterationIndex, exist := ctx.GetInt("iterationIndex") + workerIndex, exist := ctx.GetInt("workerIndex") if !exist { - return errors.New("Iteration Index does not exist in context map") + return errors.New("Worker Index does not exist in context map") } var userList, passList string @@ -74,7 +74,7 @@ func (r *rest) Login(ctx context.Context) error { } return checkTargetted(ctx, func(loginEndpoint string, apiEndpoint string) error { - return r.PostToUaaSuccessfully(fmt.Sprintf("%s/oauth/token", loginEndpoint), r.oauthInputs(credentialsForWorker(iterationIndex, userList, passList)), body, func(reply Reply) error { + return r.PostToUaaSuccessfully(fmt.Sprintf("%s/oauth/token", loginEndpoint), r.oauthInputs(credentialsForWorker(workerIndex, userList, passList)), body, func(reply Reply) error { ctx.PutString("token", body.Token) return r.targetSpace(ctx) }) @@ -246,10 +246,10 @@ func (s SpaceResponse) SpaceExists() bool { return len(s.Resources) > 0 } -func credentialsForWorker(iterationIndex int, users string, passwords string) (string, string) { +func credentialsForWorker(workerIndex int, users string, passwords string) (string, string) { var userList = strings.Split(users, ",") var passList = strings.Split(passwords, ",") - return userList[iterationIndex%len(userList)], passList[iterationIndex%len(passList)] + return userList[workerIndex%len(userList)], passList[workerIndex%len(passList)] } func (r *rest) oauthInputs(username string, password string) url.Values { diff --git a/workloads/rest_test.go b/workloads/rest_test.go index c2b5699..94cb9a8 100644 --- a/workloads/rest_test.go +++ b/workloads/rest_test.go @@ -39,7 +39,7 @@ var _ = Describe("Rest", func() { BeforeEach(func() { restContext = context.New() - restContext.PutInt("iterationIndex", 0) + restContext.PutInt("workerIndex", 0) replies = make(map[string]interface{}) replyWithLocation = make(map[string]string) client = &dummyClient{replies, replyWithLocation, make(map[call]interface{})} @@ -216,14 +216,14 @@ var _ = Describe("Rest", func() { Ω(data.(url.Values)["password"]).Should(Equal([]string{"pass1"})) }) - It("uses different username and password with different iterationIndex", func() { - restContext.PutInt("iterationIndex", 0) + It("uses different username and password with different workerIndex", func() { + restContext.PutInt("workerIndex", 0) rest.Login(restContext) data := client.ShouldHaveBeenCalledWith("POST(uaa)", "THELOGINSERVER/PATH/oauth/token") Ω(data.(url.Values)["username"]).Should(Equal([]string{"user1"})) Ω(data.(url.Values)["password"]).Should(Equal([]string{"pass1"})) - restContext.PutInt("iterationIndex", 2) + restContext.PutInt("workerIndex", 2) rest.Login(restContext) data = client.ShouldHaveBeenCalledWith("POST(uaa)", "THELOGINSERVER/PATH/oauth/token") Ω(data.(url.Values)["username"]).Should(Equal([]string{"user3"})) @@ -231,7 +231,7 @@ var _ = Describe("Rest", func() { }) It("recycles the list of username and password when there are more workers than username", func() { - restContext.PutInt("iterationIndex", 6) + restContext.PutInt("workerIndex", 6) rest.Login(restContext) data := client.ShouldHaveBeenCalledWith("POST(uaa)", "THELOGINSERVER/PATH/oauth/token") Ω(data.(url.Values)["username"]).Should(Equal([]string{"user1"})) @@ -254,19 +254,19 @@ var _ = Describe("Rest", func() { }) It("re-uses the only avaiable password", func() { - restContext.PutInt("iterationIndex", 0) + restContext.PutInt("workerIndex", 0) rest.Login(restContext) data := client.ShouldHaveBeenCalledWith("POST(uaa)", "THELOGINSERVER/PATH/oauth/token") Ω(data.(url.Values)["username"]).Should(Equal([]string{"user1"})) Ω(data.(url.Values)["password"]).Should(Equal([]string{"pass1"})) - restContext.PutInt("iterationIndex", 1) + restContext.PutInt("workerIndex", 1) rest.Login(restContext) data = client.ShouldHaveBeenCalledWith("POST(uaa)", "THELOGINSERVER/PATH/oauth/token") Ω(data.(url.Values)["username"]).Should(Equal([]string{"user2"})) Ω(data.(url.Values)["password"]).Should(Equal([]string{"pass1"})) - restContext.PutInt("iterationIndex", 2) + restContext.PutInt("workerIndex", 2) rest.Login(restContext) data = client.ShouldHaveBeenCalledWith("POST(uaa)", "THELOGINSERVER/PATH/oauth/token") Ω(data.(url.Values)["username"]).Should(Equal([]string{"user3"}))