def testAverageLoadExcludingImmatureInstance(self):
        LoadInfo.InitializeTable()
        LoadInfo.AddInstance('test-instance1')
        LoadInfo.RegisterInstanceIpAddress('test-instance1', '1.2.3.4')
        # test-instance1 hasn't yet reported load information.
        LoadInfo.AddInstance('test-instance2')
        LoadInfo.RegisterInstanceIpAddress('test-instance2', '5.6.7.8')
        LoadInfo.UpdateLoadInfo('test-instance2', 33)

        self.assertEqual((1, 33), LoadInfo.GetAverageLoad())
    def testAverageLoad(self):
        LoadInfo.InitializeTable()
        LoadInfo.AddInstance('test-instance1')
        LoadInfo.RegisterInstanceIpAddress('test-instance1', '1.2.3.4')
        LoadInfo.UpdateLoadInfo('test-instance1', 55)
        LoadInfo.AddInstance('test-instance2')
        LoadInfo.RegisterInstanceIpAddress('test-instance2', '5.6.7.8')
        LoadInfo.UpdateLoadInfo('test-instance2', 11)
        LoadInfo.AddInstance('test-instance3')
        LoadInfo.RegisterInstanceIpAddress('test-instance3', '9.10.11.12')
        LoadInfo.UpdateLoadInfo('test-instance3', 66)

        self.assertEqual((3, 44), LoadInfo.GetAverageLoad())
    def get(self):
        """Checks average load level and adjusts cluster size if necessary.

    If average load level of instances is more than upper threshold, increase
    the number of instances by 20% of original size.  If average load level is
    less than lower threshold and the current cluster size is larger than
    minimum size, decrease the number of instances by 10%.  Since shortage
    of server is more harmful than excessive instances, we increase more
    rapidly than we decrease.

    However, shutting down the instance is more complicated than adding
    instances depending on game servers.  If client is not capable to auto-
    reconnect, the game server must be drained before shutting down the
    instance.  In this exsample, decrement of the instance is not implemented.
    """
        cluster_size, average_load = LoadInfo.GetAverageLoad()
        if cluster_size:
            if average_load > self.UPPER_THRESHOLD:
                ComputeEngineController().IncreaseEngine(cluster_size / 5 + 1)
            elif (average_load < self.LOWER_THRESHOLD
                  and cluster_size > self.MIN_CLUSTER_SIZE):
                ComputeEngineController().DecreaseEngine(cluster_size / 10 + 1)