mirror of
				https://github.com/elyby/chrly.git
				synced 2025-05-31 14:11:51 +05:30 
			
		
		
		
	Ensure that queue for Mojang textures provider is initialized before any job will be scheduled
This commit is contained in:
		@@ -194,8 +194,12 @@ func (ctx *BatchUuidsProvider) GetUuid(username string) (*mojang.ProfileInfo, er
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (ctx *BatchUuidsProvider) startQueue() {
 | 
					func (ctx *BatchUuidsProvider) startQueue() {
 | 
				
			||||||
 | 
						// This synchronization chan is used to ensure that strategy's jobs provider
 | 
				
			||||||
 | 
						// will be initialized before any job will be scheduled
 | 
				
			||||||
 | 
						d := make(chan struct{})
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		jobsChan := ctx.strategy.GetJobs(ctx.context)
 | 
							jobsChan := ctx.strategy.GetJobs(ctx.context)
 | 
				
			||||||
 | 
							close(d)
 | 
				
			||||||
		for {
 | 
							for {
 | 
				
			||||||
			select {
 | 
								select {
 | 
				
			||||||
			case <-ctx.context.Done():
 | 
								case <-ctx.context.Done():
 | 
				
			||||||
@@ -208,6 +212,8 @@ func (ctx *BatchUuidsProvider) startQueue() {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						<-d
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (ctx *BatchUuidsProvider) performRequest(iteration *JobsIteration) {
 | 
					func (ctx *BatchUuidsProvider) performRequest(iteration *JobsIteration) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -105,21 +105,10 @@ type batchUuidsProviderTestSuite struct {
 | 
				
			|||||||
	Strategy  *manualStrategy
 | 
						Strategy  *manualStrategy
 | 
				
			||||||
	MojangApi *mojangUsernamesToUuidsRequestMock
 | 
						MojangApi *mojangUsernamesToUuidsRequestMock
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	GetUuidAsync func(username string) <-chan *batchUuidsProviderGetUuidResult
 | 
					 | 
				
			||||||
	stop context.CancelFunc
 | 
						stop context.CancelFunc
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (suite *batchUuidsProviderTestSuite) SetupTest() {
 | 
					func (suite *batchUuidsProviderTestSuite) GetUuidAsync(username string) <-chan *batchUuidsProviderGetUuidResult {
 | 
				
			||||||
	suite.Emitter = &mockEmitter{}
 | 
					 | 
				
			||||||
	suite.Strategy = &manualStrategy{}
 | 
					 | 
				
			||||||
	ctx, stop := context.WithCancel(context.Background())
 | 
					 | 
				
			||||||
	suite.stop = stop
 | 
					 | 
				
			||||||
	suite.MojangApi = &mojangUsernamesToUuidsRequestMock{}
 | 
					 | 
				
			||||||
	usernamesToUuids = suite.MojangApi.UsernamesToUuids
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	suite.Provider = NewBatchUuidsProvider(ctx, suite.Strategy, suite.Emitter)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	suite.GetUuidAsync = func(username string) <-chan *batchUuidsProviderGetUuidResult {
 | 
					 | 
				
			||||||
	s := make(chan struct{})
 | 
						s := make(chan struct{})
 | 
				
			||||||
	// This dirty hack ensures, that the username will be queued before we return control to the caller.
 | 
						// This dirty hack ensures, that the username will be queued before we return control to the caller.
 | 
				
			||||||
	// It's needed to keep expected calls order and prevent cases when iteration happens before
 | 
						// It's needed to keep expected calls order and prevent cases when iteration happens before
 | 
				
			||||||
@@ -144,6 +133,16 @@ func (suite *batchUuidsProviderTestSuite) SetupTest() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return c
 | 
						return c
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (suite *batchUuidsProviderTestSuite) SetupTest() {
 | 
				
			||||||
 | 
						suite.Emitter = &mockEmitter{}
 | 
				
			||||||
 | 
						suite.Strategy = &manualStrategy{}
 | 
				
			||||||
 | 
						ctx, stop := context.WithCancel(context.Background())
 | 
				
			||||||
 | 
						suite.stop = stop
 | 
				
			||||||
 | 
						suite.MojangApi = &mojangUsernamesToUuidsRequestMock{}
 | 
				
			||||||
 | 
						usernamesToUuids = suite.MojangApi.UsernamesToUuids
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						suite.Provider = NewBatchUuidsProvider(ctx, suite.Strategy, suite.Emitter)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (suite *batchUuidsProviderTestSuite) TearDownTest() {
 | 
					func (suite *batchUuidsProviderTestSuite) TearDownTest() {
 | 
				
			||||||
@@ -196,15 +195,11 @@ func (suite *batchUuidsProviderTestSuite) TestShouldNotSendRequestWhenNoJobsAreR
 | 
				
			|||||||
		close(done)
 | 
							close(done)
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	r := suite.GetUuidAsync("username") // Schedule one username to run the queue
 | 
						suite.GetUuidAsync("username") // Schedule one username to run the queue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	suite.Strategy.Iterate(0, 1) // Return no jobs and indicate that there is one job in queue
 | 
						suite.Strategy.Iterate(0, 1) // Return no jobs and indicate that there is one job in queue
 | 
				
			||||||
	select {
 | 
					
 | 
				
			||||||
	case <-r:
 | 
						<-done
 | 
				
			||||||
		// fail
 | 
					 | 
				
			||||||
	case <-done:
 | 
					 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Test written for multiple usernames to ensure that the error
 | 
					// Test written for multiple usernames to ensure that the error
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user