TEST(chatpresenter, test_incomingMessages) { Poco::SharedPtr<MockChatService> m(new MockChatService); Mvp::Presenter::TextChatPresenter presenter(m.get()); Poco::SharedPtr<MockChatView> v(new MockChatView()); EXPECT_CALL(*v, initialize()); ON_CALL(*m, getParticipants()) .WillByDefault(Return(contacts)); ON_CALL(*m, messageHistory(_)) .WillByDefault(Return(messages)); EXPECT_CALL(*m, chatName()); EXPECT_CALL(*v, setChatTitle(_)); EXPECT_CALL(*m, messageHistory(_)); EXPECT_CALL(*m, isConsumer()); EXPECT_CALL(*v, enableControls(_)); EXPECT_CALL(*v, showLeaveAction(_)); EXPECT_CALL(*m, getParticipants()); EXPECT_CALL(*v, setParticipants(_)); presenter.setView(v.get()); EXPECT_CALL(*v, showView()); presenter.showView(); EXPECT_CALL(*v, addMessage(_)); m->triggerIncomingMessage(); }
void* routine(void* info) { struct thInfo* thinfo = (struct thInfo*) info; if(isConsumer(&thinfo->flags)) { CriticalAdd(&cCountLock, &cCount, 1); int r=0; while(r == 0 && getAmount(&thinfo->flags) > 0 ) { r = Consumer(thinfo->persist, ConsumerAction, thinfo); } CriticalAdd(&cCountLock, &cCount, -1); } if(isProducer(&thinfo->flags)) { CriticalAdd(&pCountLock, &pCount, 1); int r=0; while(r == 0 && getAmount(&thinfo->flags) > 0 ) { r = Producer(thinfo->persist, ProducerAction, thinfo); } CriticalAdd(&pCountLock, &pCount, -1); } return NULL; }
pair<Date, double> FlowEnd::getFlowplanDateQuantity(const FlowPlan* fl) const { if (isConsumer() && !fl->getOperationPlan()->getConsumeMaterial()) return make_pair(fl->getOperationPlan()->getEnd(), 0.0); else if (isProducer() && !fl->getOperationPlan()->getProduceMaterial()) return make_pair(fl->getOperationPlan()->getEnd(), 0.0); else if (fl->getConfirmed()) return make_pair( fl->getOperationPlan()->getEnd(), fl->getQuantity() ); else return make_pair( fl->getOperationPlan()->getEnd(), getEffective().within(fl->getDate()) && fl->getOperationPlan()->getQuantity() ? getQuantityFixed() + fl->getOperationPlan()->getQuantity() * getQuantity() : 0.0 ); }
TEST(chatpresenter, test_postingsms) { Poco::SharedPtr<MockChatService> m(new MockChatService); Mvp::Presenter::TextChatPresenter presenter(m.get()); Poco::SharedPtr<MockChatView> v(new MockChatView()); EXPECT_CALL(*v, initialize()); ON_CALL(*m, getParticipants()) .WillByDefault(Return(contacts)); ON_CALL(*m, messageHistory(_)) .WillByDefault(Return(messages)); EXPECT_CALL(*m, chatName()); EXPECT_CALL(*v, setChatTitle(_)); EXPECT_CALL(*m, messageHistory(_)); EXPECT_CALL(*m, isConsumer()); EXPECT_CALL(*v, enableControls(_)); EXPECT_CALL(*v, showLeaveAction(_)); EXPECT_CALL(*m, getParticipants()); EXPECT_CALL(*v, setParticipants(_)); presenter.setView(v.get()); EXPECT_CALL(*v, showView()); presenter.showView(); EXPECT_CALL(*v, setSmsMode(true)); v->triggerToggleMode(); EXPECT_CALL(*m, postSMS(_)); v->triggerPostMessage(); EXPECT_CALL(*v, reportMessageStatus( Mvp::View::AbstractChatView::SmsDelivered, _)); m->triggerSmsDelivered(); EXPECT_CALL(*v, reportMessageStatus( Mvp::View::AbstractChatView::MiscError, _)); m->triggerSmsError(); }
pair<Date, double> FlowTransferBatch::getFlowplanDateQuantity(const FlowPlan* fl) const { double batch_quantity = getTransferBatch(); if (!batch_quantity || fl->getOperationPlan()->getSetupEnd() == fl->getOperationPlan()->getEnd()) // Default to a simple flowplan at the start or end return make_pair( isConsumer() ? fl->getOperationPlan()->getSetupEnd() : fl->getOperationPlan()->getEnd(), getQuantityFixed() + getQuantity() * fl->getOperationPlan()->getQuantity() ); // Compute the number of batches double total_quantity = getQuantityFixed() + fl->getOperationPlan()->getQuantity() * getQuantity(); if (isConsumer() && !fl->getOperationPlan()->getConsumeMaterial()) total_quantity = 0.0; else if (isProducer() && !fl->getOperationPlan()->getProduceMaterial()) total_quantity = 0.0; double batches = ceil((getQuantity() > 0 ? total_quantity : -total_quantity) / getTransferBatch()); if (!batches) batches = 1; else if (batches > 50) { // Put a limit to the number of batches batches = 50; batch_quantity = (getQuantity() > 0 ? total_quantity : -total_quantity) / 50; } // Count the index of this batch bool found = false; long count = 0; long totalcount = 0; FlowPlan* cur_flpln = fl->getOperationPlan()->firstflowplan; FlowPlan* prev_flpln = nullptr; while (cur_flpln) { if (cur_flpln == fl) found = true; if (cur_flpln->getFlow() == fl->getFlow()) { ++totalcount; if (totalcount > batches && !count) { if (cur_flpln->oper->firstflowplan == cur_flpln) cur_flpln->oper->firstflowplan = cur_flpln->nextFlowPlan; else prev_flpln->nextFlowPlan = cur_flpln->nextFlowPlan; auto almost_dead = cur_flpln; cur_flpln = cur_flpln->nextFlowPlan; delete almost_dead; continue; } if (!found) ++count; } prev_flpln = cur_flpln; cur_flpln = cur_flpln->nextFlowPlan; } Duration op_delta; Date op_date = fl->getOperation()->calculateOperationTime( fl->getOperationPlan(), fl->getOperationPlan()->getSetupEnd(), fl->getOperationPlan()->getEnd(), &op_delta ).getStart(); if (!count) { // The first flowplan in the list will always be there, even when the quantity becomes 0. // It is responsible for creating extra flowplans when required. while (totalcount < batches) { auto newflowplan = new FlowPlan(fl->getOperationPlan(), this); newflowplan->setFollowingBatch(true); ++totalcount; } } if (getQuantity() > 0 || getQuantityFixed() > 0) { // Producing a batch op_delta = static_cast<long>(op_delta) / static_cast<long>(batches) * (count + 1); total_quantity -= count * batch_quantity; if (total_quantity < 0.0) total_quantity = 0.0; return make_pair( fl->getOperation()->calculateOperationTime( fl->getOperationPlan(), op_date, op_delta, true ).getEnd(), total_quantity > batch_quantity ? batch_quantity : total_quantity ); } else { // Consuming a batch op_delta = static_cast<long>(op_delta) / static_cast<long>(batches) * count; total_quantity += count * getTransferBatch(); if (total_quantity > 0.0) total_quantity = 0.0; return make_pair( fl->getOperation()->calculateOperationTime( fl->getOperationPlan(), op_date, op_delta, true ).getEnd(), total_quantity < -batch_quantity ? -batch_quantity : total_quantity ); } }
int main(int argc, char* const argv[]) { srand(time(NULL)); int n=10, m=255 & AmountMask, p=GRAIN, c = n*m/4; int gopt; while((gopt = getopt(argc,argv,"n:m:p:c:")) != -1) { switch(gopt) { case 'n': n = atoi(optarg); break; case 'm': m = atoi(optarg); break; case 'p': p = atoi(optarg); break; case 'c': c = atoi(optarg); break; } } if(m > (255 & AmountMask)) { printf("Max m is %d\n", (255 & AmountMask)); } printf("n: %d (thread total nr) m: %d (max production/consumption) p: %d (chance) c: %d (maxItems)\n", n,m,p,c); pthread_t threads[n]; memset(threads, 0, sizeof(threads)); pthread_attr_t pattr; CERRS(pthread_attr_init(&pattr), "pthread attr init failed"); CERRS(pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE), "pthread attr setds failed"); struct thInfo* thinfo; thinfo = (struct thInfo*) malloc(sizeof(struct thInfo)*n); memset(thinfo, 0, n*sizeof(struct thInfo)); struct thInfoPersist* pr = (struct thInfoPersist*)malloc(sizeof(struct thInfoPersist)); CERRS(pthread_mutex_init(&pr->buffer,NULL) != 0, "sem init failed"); CERRS(pthread_cond_init(&pr->notEmpty,NULL) != 0, "cond init failed"); CERRS(pthread_cond_init(&pr->notFull,NULL) != 0, "cond init failed"); CERRS(pthread_mutex_init(&pCountLock,NULL) != 0, "mutex init failed"); CERRS(pthread_mutex_init(&cCountLock,NULL) != 0, "mutex init failed"); pr->maxItems = c; pr->itemCount = 0; int balance = pr->itemCount; cCount=0; pCount=0; for(int i=0; i < n; i++) { setThInfo(&thinfo[i], pr); setAmount(&thinfo[i].flags, rand() % m); if(rand() % (p+GRAIN) > GRAIN ) { printf("Made consumer: "); setConsumer(&thinfo[i].flags, 1); balance = balance - getAmount(&thinfo[i].flags); } else { printf("Made producer: "); setProducer(&thinfo[i].flags, 1); balance = balance + getAmount(&thinfo[i].flags); } printf("am : %u bal: %d IC: %d\n", getAmount(&thinfo[i].flags), balance, pr->itemCount); CERRS(pthread_create(&threads[i], &pattr, &routine, (void*) &thinfo[i]), "pthread creation failed"); } for (int j=0; j < n; j++) { // printf("\n joining. %d p(%d) c(%d)\n", j, pCount, cCount); CERRS(pthread_join(threads[j], NULL), "pthread join failed"); printf("\nnr %d (%s) done\n", j, (isConsumer(&(thinfo[j].flags))) ? "Consumer" : "Producer"); } free(thinfo); free(pr); }