Beispiel #1
0
 public FactoryAndPoolOfItsAsyncObjects(
     Func <F> createFactoryInstance,
     Func <F, Task> destroyFactoryInstanceAsync,
     int maxPooledObjects,
     Func <F, O> createObject,
     Func <O, Task> destroyObjectAsync,
     Func <O, bool> validateObjectInstance)
 {
     _destroyFactoryInstanceAsync = destroyFactoryInstanceAsync;
     Factory     = createFactoryInstance();
     ObjectsPool = new PoolOfAsyncThings <O>(
         maxSize: maxPooledObjects,
         createInstance: () =>
     {
         // PoolOfThings lets us use the instances while destroying them
         // DestroyAsync may null Factory out so we save a local copy
         var f = Factory;
         return(f != null ? createObject(f) : null);
     },
         destroyInstanceAsync: async(o) => await destroyObjectAsync(o),
         instanceValidator: validateObjectInstance);
 }
 static PooledFactoriesAndChannelsAsync()
 {
     s_test = new TestTemplate();
     // we do not expect exceptions here since we're not closing anything during usage
     (s_test as IExceptionPolicy).RelaxedExceptionPolicy = false;
     s_pooledFactoriesAndChannels = StaticDisposablesHelper.AddDisposable(
         new PoolOfAsyncThings <FactoryAndPoolOfItsAsyncObjects <ChannelFactory <ChannelType>, ChannelType> >(
             maxSize: s_test.TestParameters.MaxPooledFactories, // # of pooled FactoryAndPoolOfItsObjects
             createInstance: () => new FactoryAndPoolOfItsAsyncObjects <ChannelFactory <ChannelType>, ChannelType>(
                 createFactoryInstance:
                 s_test.CreateChannelFactory,
                 destroyFactoryInstanceAsync: async(chf) =>
                 await s_test.CloseFactoryAsync(chf),
                 maxPooledObjects: s_test.TestParameters.MaxPooledChannels, // # of pooled channels within each pooled FactoryAndPoolOfItsObjects
                 createObject: (chf) =>
                 s_test.CreateChannel(chf),
                 destroyObjectAsync: async(ch) =>
                 await s_test.CloseChannelAsync(ch),
                 validateObjectInstance: s_test.ValidateChannel
                 ),
             destroyInstanceAsync: async(fapoiao) => await fapoiao.DestroyAsync(),
             instanceValidator: (fapoiao) => s_test.ValidateFactory(fapoiao.Factory)));
 }
Beispiel #3
0
 public AllPooledInstancesCollection(PoolOfAsyncThings <T> thePool)
 {
     _thePool = thePool;
 }
        public static async Task CreateFactoriesAndChannelsUseAllOnceCloseAllAsync()
        {
            PoolOfAsyncThings <FactoryAndPoolOfItsAsyncObjects <ChannelFactory <ChannelType>, ChannelType> > oneTimeAsyncThing = null;
            var allTasksFromOneTimeAsyncThing = new List <Task>();

            try
            {
                oneTimeAsyncThing = new PoolOfAsyncThings <FactoryAndPoolOfItsAsyncObjects <ChannelFactory <ChannelType>, ChannelType> >(
                    maxSize: s_test.TestParameters.MaxPooledFactories, // # of pooled FactoryAndPoolOfItsObjects
                    createInstance: () => new FactoryAndPoolOfItsAsyncObjects <ChannelFactory <ChannelType>, ChannelType>(
                        createFactoryInstance: () =>
                        s_test.CreateChannelFactory(),
                        destroyFactoryInstanceAsync: async(chf) =>
                        await s_test.CloseFactoryAsync(chf),
                        maxPooledObjects: s_test.TestParameters.MaxPooledChannels, // # of pooled channels within each pooled FactoryAndPoolOfItsObjects
                        createObject: (chf) =>
                        s_test.CreateChannel(chf),
                        destroyObjectAsync: async(ch) =>
                        await s_test.CloseChannelAsync(ch)
                        ),
                    destroyInstanceAsync: (_fapoiao) => _fapoiao.DestroyAsync());

                foreach (var factoryAndPoolOfItsChannels in oneTimeAsyncThing.GetAllPooledInstances())
                {
                    foreach (var channel in factoryAndPoolOfItsChannels.ObjectsPool.GetAllPooledInstances())
                    {
                        allTasksFromOneTimeAsyncThing.Add(s_test.UseAsyncChannel()(channel));
                    }
                }
            }
            finally
            {
                if (oneTimeAsyncThing != null)
                {
                    await Task.WhenAll(allTasksFromOneTimeAsyncThing);

                    await oneTimeAsyncThing.DestoryAllPooledInstancesAsync();
                }
            }
        }